index()

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.

Check if the search string is within our main string, if found then the position of matching string is returned. If not found then this method raise an exception.
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

Case sensitive

index() is a case sensitive search
my_str="Welcome to plus2net.com"
print(my_str.index('CO'))      # ValueError: substring not found

index() and find()

We can use find() method also to search the position but find() method returns -1 if not found but index() method raise an exception if not found.

All String methods


Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer