Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In this tutorial, we'll learn about the index()
method in Python, which is a string method that searches for a specified substring within a string and returns the index of the first occurrence. If the substring is not found, the method raises a ValueError
.
The index()
method is similar to the find()
method, but raises an error when the substring is not found instead of returning -1.
The index()
method takes the following arguments:
sub
(required): The substring to search for within the string.start
(optional): The starting index from which to search for the substring. By default, it is 0.end
(optional): The ending index up to which to search for the substring. By default, it is the length of the string.Example 1: Basic usage of the index()
method
text = "Hello, world! Welcome to the world of Python." substring = "world" index = text.index(substring) print(index) # Output: 7
Example 2: Using start
and end
arguments
text = "Hello, world! Welcome to the world of Python." substring = "world" index = text.index(substring, 10) # Start searching from index 10 print(index) # Output: 28
text = "Hello, world! Welcome to the world of Python." substring = "world" index = text.index(substring, 0, 10) # Search between index 0 and 10 print(index) # Output: 7
Example 3: Substring not found
text = "Hello, world! Welcome to the world of Python." substring = "Java" try: index = text.index(substring) except ValueError: print(f"'{substring}' not found in the text.")
Note that the index()
method is case-sensitive. If you want to perform a case-insensitive search, you can convert both the text and the substring to lowercase (or uppercase) before using the index()
method.
text = "Hello, world! Welcome to the World of Python." substring = "WORLD" index = text.lower().index(substring.lower()) print(index) # Output: 28
In summary, the index()
method is used to search for a specified substring within a string and returns the index of the first occurrence. If the substring is not found, it raises a ValueError
. You can also specify a start and end index to search within a specific range.
How to Use index()
to Check Substring Presence in Python:
index()
method is used to find the index of the first occurrence of a substring in a string. If the substring is not found, it raises a ValueError
.# Example sentence = "Python is powerful" try: index = sentence.index("is") except ValueError: index = -1
Checking for Substring Existence with index()
in Python:
index()
to check if a substring exists in a string.# Example sentence = "Python programming" try: index = sentence.index("java") except ValueError: index = -1 if index != -1: print("Substring found") else: print("Substring not found")
Find Substring Index in a String with Python index()
:
index()
.# Example sentence = "Python is versatile" try: index_python = sentence.index("Python") except ValueError: index_python = -1
Using index()
to Search for a Substring Occurrence in Python:
index()
.# Example sentence = "Python programming is fun. Python is powerful." try: index_second_python = sentence.index("Python", sentence.index("Python") + 1) except ValueError: index_second_python = -1
Handling Missing Substrings with index()
in Python:
# Example sentence = "Python is amazing" substring = "Java" try: index = sentence.index(substring) except ValueError: index = -1 if index != -1: print(f"Substring found at index {index}") else: print(f"Substring '{substring}' not found")
Case-Insensitive Substring Search with index()
in Python:
lower()
.# Example sentence = "Python is CASE-INSENSITIVE" substring = "case-insensitive" try: index = sentence.lower().index(substring.lower()) except ValueError: index = -1
Python index()
vs find()
for Substring Detection:
find()
returns -1 for missing substrings, index()
raises a ValueError
.# Example with find() sentence = "Python is great" index_find = sentence.find("Java") # Returns -1 if not found # Example with index() try: index_index = sentence.index("Java") # Raises ValueError if not found except ValueError: index_index = -1
Detecting Multiple Substring Occurrences with index()
in Python:
index()
.# Example sentence = "Python is easy. Python is versatile. Python is powerful." substring = "Python" start_index = 0 while True: try: index = sentence.index(substring, start_index) print(f"Found at index: {index}") start_index = index + len(substring) except ValueError: break