In this tutorial, we’ll learn what Python string index() method is and how to properly use it with the help of an easy and detailed Python code examples.
Introduction: Python String index() Method
This method is used to search a specific substring inside a string and return index of its first character(only if the specified substring is found). If not, then it’ll raise an exception.
Let’s now first understand its syntax, then we’ll practically implement it.
Syntax of index() Method
string.index(subString, startPoint, endPoint)
- Python string index method takes three arguments.
- First one is the substring that will be searched in a specified string.
- Second argument specifies the starting point from where the search will execute and third argument specifies the ending point where search will stop.
- This method returns the index of first character of substring inside a string(if found). If not, then it’ll raise a ValueError exception.
Example 1: Get Index of Substring using index() Method
val='This is a Python Language' print(val.index('is a')) // 5 print(val.index('is')) // 2 it will take is of (This) substring print(val.upper().index('IS A')) // 5 print(val.lower().index('is a')) // 5
Output
5 2 5 5
As you can see in the above code that we’ve tried Python string index method with different substrings.
In order to avoid case sensitivity, we can either use lower() or upper() method of string like shown in the above image. It means if we pass ‘python’ to index method and only if Python(uppercase) is present then Value error will be raised. According to index() method, the specified substring is not present as its case sensitive.
Just make sure that while using upper method, the substring should be uppercase as well. Same goes for lower() method(lowercase substring).
Example 2: Substring Not Found
val='This is a Python Language' print(val.index('programming'))
Output
ValueError: substring not found
Python string index() method raised a ValueError exception as substring is not found in the above specified string.
Example 3: Starting And Ending Range of String
val='This is a Python Language' print(val.index('Python',16))
Output
ValueError: substring not found
Substring is present but still value error exception is raised. Reason is that the starting point of search comes after the substring.
We can also specify an ending range/point as well. See below code:
val='This is a Python Language' print(val.index('Python',5,17))
Output
10
By specify a starting and ending point, we can easily create a range/limit in string within which the search for substring will be executed.
Example 4: index() Method Return Value
val='This is a Python Language' returnVal=val.index('Language') print(returnVal)
Output
17
Python string index() method returns the index of first character of substring(if found in the specified string). If not, then it raises a ValueError exception.
Conclusion
To conclude this tutorial, hope you now have a detailed practical knowledge of how to properly use Python string index method. I’ll be looking forward to have your valuable feedback. Thank you for reading this post.