my_str.index(search_string,start,end))
search_string : String to be checked inside the main string. start: Optional , search to start from this position end : Optional , search to end from this position. my_str="Welcome to plus2net.com"
print(my_str.index('co')) # Output is 3
print(my_str.index('co',4)) # Output is 20
print(my_str.index('co',4,18)) # ValueError: substring not found
index() is a case sensitive search
my_str="Welcome to plus2net.com"
print(my_str.index('CO')) # ValueError: substring not found
find() method returns -1 if not found but index() method raise an exception if not found.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.