Skip to content

How to find position of a character in a string in Python (in 4 ways)

How to find position of a character in a string in python

In this tutorial we will learn how to find position of a character in a string in Python . Python string provides some built-in methods to find position of a character in a string . They are as follows –

  • Python string find() method
  • Python string rfind() method
  • Python string index() method
  • Python string rindex() method

Here we will use the string as “Coding Conception” . From the below image we can see what is the position of every character in the string .

Now we will discuss about all the previously mentioned methods with examples –

Python string find() method

It will return the index number for the first occurrence of a character in a string . If the character is not found then it will return -1 .

Syntax of find() method

string.find(sub, start ,end)

Parameters of find() method

  • sub = It is the substring whose position we want to know in the string .
  • start = Optional . It is an integer number specifying from where find() method will start searching . This number is included and its default value is 0 .
  • end = Optional . It is an integer number specifying the position where find() method will end searching . This number is excluded and its default value is length of the string .

Return type of find() method

It returns an integer number .

Examples of find() method

Example 1:

mystring = "Coding Conception"

# finding the position of first "C"
print("position of first 'C' =",mystring.find("C"))

# finding the position of first "on"
print("position of first 'on' =",mystring.find("on"))
position of first 'C' = 0
position of first 'on' = 8

Example 2:

# finding the position of first "C" starting from 4 to 10
print("position of first 'C' from 4 to 10 =",mystring.find("C",4,10))

# finding the position of first "on" starting from 10 to end
print("position of first 'on' from 10 to end =",mystring.find("on",10))
position of first 'C' from 4 to 10 = 7
position of first 'on' from 10 to end = 15

Example 3:

# if character is not found
print("position of 'A' =",mystring.find("A"))
position of 'A' = -1

Python string rfind() method

It will return the index number for the last occurrence of a character in a string . If the character is not found then it will return -1 . Actually rfind() method iterates a string from right side .

Syntax of rfind() method

string.rfind(sub, start ,end)

Parameters of rfind() method

  • sub = It is the substring whose position we want to know in the string .
  • start = Optional . It is an integer number specifying from where rfind() method will start searching . This number is included and its default value is 0 .
  • end = Optional . It is an integer number specifying the position where rfind() method will end searching . This number is excluded and its default value is length of the string .

Return type of rfind() method

It returns an integer number .

Examples of rfind() method

Example 1:

mystring = "Coding Conception"

# finding the position of last "C"
print("position of last 'C' =",mystring.rfind("C"))

# finding the position of last "on"
print("position of last 'on' =",mystring.rfind("on"))
position of last 'C' = 7
position of last 'on' = 15

Example 2:

# finding the position of last "C" starting from 0 to 2
print("position of last 'C' from 0 to 2 =",mystring.rfind("C",0,2))

# finding the position of last "on" starting from 0 to 10
print("position of last 'on' from 0 to 10 =",mystring.rfind("on",0,10))
position of last 'C' from 0 to 2 = 0
position of last 'on' from 0 to 10 = 8

Example 3:

# if character is not found
print("position of 'A' =",mystring.rfind("A"))
position of 'A' = -1

Python string index() method

It will return the index number for the first occurrence of a character in a string . If the character is not found then it will raise ValueError .

Syntax of index() method

string.index(sub, start ,end)

Parameters of index() method

  • sub = It is the substring whose position we want to know in the string .
  • start = Optional . It is an integer number specifying from where index() method will start searching . This number is included and its default value is 0 .
  • end = Optional . It is an integer number specifying the position where index() method will end searching . This number is excluded and its default value is length of the string .

Return type of index() method

It returns an integer number .

Examples of index() method

Example 1:

mystring = "Coding Conception"

# finding the position of first "C"
print("position of first 'C' =",mystring.index("C"))

# finding the position of first "on"
print("position of first 'on' =",mystring.index("on"))
position of first 'C' = 0
position of first 'on' = 8

Example 2:

# finding the position of first "C" starting from 4 to 10
print("position of first 'C' from 4 to 10 =",mystring.index("C",4,10))

# finding the position of first "on" starting from 10 to end
print("position of first 'on' from 10 to end =",mystring.index("on",10))
position of first 'C' from 4 to 10 = 7
position of first 'on' from 10 to end = 15

Example 3:

# if character is not found
print("position of 'A' =",mystring.index("A"))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_3724/138207988.py in <module>
      1 # if character is not found
----> 2 print("position of 'A' =",mystring.index("A"))

ValueError: substring not found

Python string rindex() method

It will return the index number for the last occurrence of a character in a string . If the character is not found then it will raise ValueError . Actually rindex() method iterates a string from right side .

Syntax of rindex() method

string.rindex(sub, start ,end)

Parameters of rindex() method

  • sub = It is the substring whose position we want to know in the string .
  • start = Optional . It is an integer number specifying from where rindex() method will start searching . This number is included and its default value is 0 .
  • end = Optional . It is an integer number specifying the position where rindex() method will end searching . This number is excluded and its default value is length of the string .

Return type of rindex() method

It returns an integer number .

Examples of rindex() method

Example 1:

mystring = "Coding Conception"

# finding the position of last "C"
print("position of last 'C' =",mystring.rindex("C"))

# finding the position of last "on"
print("position of last 'on' =",mystring.rindex("on"))
position of last 'C' = 7
position of last 'on' = 15

Example 2:

# finding the position of last "C" starting from 0 to 2
print("position of last 'C' from 0 to 2 =",mystring.rindex("C",0,2))

# finding the position of last "on" starting from 0 to 10
print("position of last 'on' from 0 to 10 =",mystring.rindex("on",0,10))
position of last 'C' from 0 to 2 = 0
position of last 'on' from 0 to 10 = 8

Example 3:

# if character is not found
print("position of 'A' =",mystring.rindex("A"))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_3724/2863189514.py in <module>
      1 # if character is not found
----> 2 print("position of 'A' =",mystring.rindex("A"))

ValueError: substring not found

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply