Python String Index Method

In python, the string index() method is useful to search the string for specified substring and return the index position of the first occurrence of the specified substring. In case if the specified substring/character is not found, it will raise an exception.

 

Following is the pictorial representation of string index() method functionality in python.

 

Python string index method example result

 

If you observe the above diagram, we are trying to find the "Hi" word in the given string “Hi Guest Hi” using the index function in python, and it returned the index position of the first occurred specified substring as 0. In python, the index position will always start from 0, so it returned the index position 0.

 

In python, the string index() method is same as the string find() method, but the only difference is the string find() method will return -1 in case if the specified substring/character is not found, but the index() method will raise an exception.

String Index Method Syntax

Following is the syntax of defining a string index method in python.

 

string.index(string/char, startIndex, endIndex)

If you observe the above syntax, the index() method accepts three parameters (string/char, startIndex, endIndex). Here, startIndex and endIndex parameters are optional.

 

  • string/char - It’s the substring/character you want to search in the string.
  • startIndex - It’s optional and indicates from which index position the search can start. By default, it's 0.
  • endIndex - It’s optional and indicates at which index position the search can end. By default, it's the end of the string.

String Index Method Example

Following is the example of searching the specified substring/character in the given string using the index() method in python.

 

msg = "Hi Guest hi, welcome to tutlane"
rmsg1 = msg.index("hi")
print("Substring 'hi':", rmsg1)
rmsg2 = msg.index("Python")
print("Substring 'Python':", rmsg2)

If you observe the above example, we are searching the required substrings in the given string using index() method without specifying start/end index parameters.

 

When you execute the above python program, you will get the result as shown below.

 

Substring 'hi': 9
Traceback (most recent call last):
  File "D:\Python Examples\pythonmethods.py", line 4, in <module>
    rmsg2 = msg.index("Python")
ValueError: substring not found

As discussed in the earlier chapters, python is a case sensitive language due to that the substring 'hi' was found at index position 9 even though the string starts with 'Hi'. For another substring 'Python' it returned an exception because the specified substring was not found in the string.

 

In the above example, we didn’t mention any start/end index positions to search for the specified substrings. By default, the string index() method will search the complete string for the specified substrings/characters.

 

If you want to limit the search range, you need to mention the start and end index parameter values in the index() method.

 

Following is the example of searching the substrings/characters within the specified index positions range using the index() method.

 

msg = "Hi Guest hi, welcome to tutlane"
rmsg1 = msg.index("hi",1,15)
print("Substring 'hi':", rmsg1)
rmsg2 = msg.index("Guest", 2, 8)
print("Substring 'Guest':", rmsg2)
rmsg3 = msg.index("to",4,8)
print("Substring 'to':", rmsg3)

If you observe the above example, we are limiting the string search by specifying the start and end index positions in the index() method.

 

When you execute the above python program, you will get the result as shown below.

 

Substring 'hi': 9
Substring 'Guest': 3
Traceback (most recent call last):
  File "D:\Python Examples\pythonmethods.py", line 6, in <module>
    rmsg3 = msg.index("to",4,8)
ValueError: substring not found

This is how you can use the string index() method in python to search for the specified substring/character in the given string based on your requirements.