Python String index() Method
The index()
method is used to find the index of the first occurrence of a specified substring within a string. If the substring is not found, it raises a ValueError
.
Syntax
string.index(substring, start, end)
Parameters
substring
: The substring to search for.
start
(Optional): The starting index to search from. Default is 0
.
end
(Optional): The ending index to stop the search (not inclusive). Default is the end of the string.
Return Value
Returns the index of the first occurrence of the substring. Raises ValueError
if the substring is not found.
Examples
Basic Usage
text = "hello world"
index = text.index("world")
print(index) # Output: 6
If the Substring is not Found
The index()
method raises a ValueError
, if the substring is not found within the string.
text = "hello world"
index = text.index("monkey") # Raises ValueError
Handling Exception
You can easily handle this error using the try-except
block.
text = "hello world"
try:
index = text.index("monkey")
print(index)
except ValueError:
print("Value not found")
Output:
Value not found
With start
Parameter
You provide the start
index if you want to search for a substring starting from a specific index to the end of the string.
text = "python is easy, python is fun"
index = text.index("python", 1)
print(index) # Output; 16
In this example, the substring "python"
is at index 0
and 16
within the string text
. By passing 1
as the start
parameter to the index()
method, it skips the substring "python"
at index 0
and searches for the next occurrence of the substring "python"
within the range 1
to the end of the string. With this range the substring "python"
appears at index 16
therefore the index()
method returns 16
.
With start
and end
Parameters
You provide the start
and end
parameters to the index()
method if you want to search for a substring within a specific range of the string.
text = "hello hello hello"
index = text.index("hello", 1, 6) # Raises ValueError
print(index)
In this example, the substring "hello"
is at index 0
, 6
, and 12
within the string text
. By passing 1
as the start
and 6
as the end
parameters to the index()
method, it searches for the substring "hello"
within the range 1
and 6
(exclusive). Since there is no substring "hello"
within this range, the index()
method raises ValueError
.
Finding a Single Character
You can also use the index()
method to find the index of the first occurrence of a single character within a string.
text = "hello"
index = text.index("l")
print(index) # Output: 2
Case-Sensitive
The index()
method is case-sensitive, meaning it treats uppercase and lowercase letters as distinct characters.
text = "Python is awesome"
index = text.index("python") # Raises ValueError
print(index)
In this example, the index()
method raises ValueError
because it is case-sensitive. It looks for the exact lowercase "python"
within the string text
. But the text
contains "Python"
with an uppercase "P"
. Since the cases don’t match, the index()
method raises a ValueError
.
index()
VS find()
Both index()
and find()
methods are used to find the index of the first occurrence of a substring within a string. The key difference is their behavior when a substring is not found:
index()
: Raises a ValueError
find()
: Returns -1