4.2 index() and find() method - Python

mohit355

Mohit Raj

Posted on December 23, 2019

4.2 index() and find() method - Python

Indexing in Python string

Strings are ordered sequences of character data. Indexing allows you to access individual characters in a string directly by using a numeric value. String indexing is zero-based: the first character in the string has index 0, the next is 1, and so on. In this post, I’ll show you string indexing syntax and practice with several examples
Alt Text

Alt Text

From the above figure-

  1. first character in the string has index 0, the next is 1, and so on.
  2. If start indexing from the last character then you have to start with -1 and as you come near to first character negative value increases by 1 each time. this is also called negative indexing.

How to access a character in string:

Simple answer is by using the index of all the characters in the string.

Example:

s='Hello'
#if i want to access 'H' in Hello then 
print("First character is : "s[0])      # indexing stated from 0

# if i want to access 'o' in s
print(" Last character using positive indexing : ",s[4])

#another way
print("last charcater using negative indexing : ",s[-1])    # using negative indexing
Enter fullscreen mode Exit fullscreen mode

Output is:

First character is : H
Last character using positive indexing : o
last charcater using negative indexing : o
Enter fullscreen mode Exit fullscreen mode

If we pass a index value which not exist for that string then it will return an error

s='python'
print("Third character is : ",s[2])
print("Seven character is : ",s[7])    # which is not possible for given string
Enter fullscreen mode Exit fullscreen mode

Output gives an error for the second value

Third character is : t
Seven character is : 
IndexError: string index out of range
Enter fullscreen mode Exit fullscreen mode

Python String index() Method

  1. The index() method finds the first occurrence of the specified value.
  2. The index() method raises an exception if the value is not found.

Syntax

     string.index(value, start, end)
Enter fullscreen mode Exit fullscreen mode

value(Required.) -The string to be searched for.

start(Optional.)- This function specifies the position
from where search has to be started. Default is 0

end(Optional.) - This function specifies the
position from where search has to end. Default is to the end of the string

Return the starting index number of the passed valued according to the start and end value passed.

By default start value is 0 and end value is string length.

Example

sentence='hello world and hello python'
value='h'
x=sentence.index(value)   # start and end is default value
print(x)

y=sentence.index(value,2)  # end value is default
print(y)

z=sentence.index(value,6,8)   # give error because in between 6 and 8 'h' not exist.
print(z)

value='and'
a=sentence.index(value,6,20)
print(a)
Enter fullscreen mode Exit fullscreen mode

Output is :

0
16
ValueError: substring not found
12
Enter fullscreen mode Exit fullscreen mode

Python String find() Method

  1. The find() method finds the first occurrence of the specified value.
  2. The find() method returns -1 if the value is not found.
  3. The find() method is almost the same as the index() method, the only difference is that the index() method raises an exception if the value is not found.
  4. If value is not found find() method return -1.

Syntax

string.find(value, start, end)
Enter fullscreen mode Exit fullscreen mode

value(Required.) -The string to be searched for.

start(Optional.)- This function specifies the position
from where search has to be started. Default is 0

end(Optional.) - This function specifies the
position from where search has to end. Default is to the end of the string

Example:

sentence='hello world and hello python'
value='h'
x=sentence.index(value)   # start and end is default value
print(x)

y=sentence.index(value,2)  # end value is default
print(y)

z=sentence.index(value,6,8)   #  Retuen -1 because value not found between 6 and 8.
print(z)

value='and'
a=sentence.index(value,6,20)
print(a)
Enter fullscreen mode Exit fullscreen mode

Output is :

0
16
-1
12
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
mohit355
Mohit Raj

Posted on December 23, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Learning Python
javascript Learning Python

November 28, 2024

Calculate savings with Python!
beginners Calculate savings with Python!

November 26, 2024

UV the game-changer package manager
programming UV the game-changer package manager

November 24, 2024

Beginners Guide for Classes
python Beginners Guide for Classes

November 20, 2024