Python String index()

The Python String index() method is a built-in function that returns the substring’s lowest index (first occurrence) in a given string. If not found, it raises ValueError: substring not found exception.

In this article, we will learn about the Python string index() method with the help of examples.

index() Syntax

The Syntax of index() method is:

str.index(sub, start, end)

index() Parameters

The index() method can take three parameters.

  • sub – substring that needs to be searched in the given string.
  • start (optional) – starting position where the substring needs to be searched within the string
  • end (optional) – ending position where the substring needs to be searched within the string
Note: If the start and end indexes are not provided, by default, it takes 0 as the starting index and str.length-1 as the end index

index() Return Value

The index() method returns an integer value, an index of a substring.

  • If the substring is found in the given string, the index() method will return the index of the first occurrence of the substring.
  • If the substring is not found in the given string, it throws ValueError: substring not found exception.

Difference between index() method and find() method

The index() method is similar to find() method. The only major difference is that the find() method returns -1 if the substring is not found in a given string, whereas the index() method will raise the ValueError: substring not found exception.

Example 1: Find the index of a string in Python

The index in Python starts from 0 and not from 1. Hence in the below example the first occurence of the substring ‘Code‘ is found at index position 5.

text = 'ItsMyCode - Learn how to Code in Python '

# find the index of first occurence of substring
output = text.index('Code')
print("The index of first occurrence of 'Code' is:", output)

# find the index of character
output = text.index('-')
print("The index of first occurrence of '-' is:", output)

Output

The index of first occurrence of 'Code' is: 5
The index of first occurrence of '-' is: 10

Example 2: If string is not found ValueError: substring not found exception

Here the substring ‘Hello’ is not found in the given string and hence Python interpreter raises ValueError: substring not found exception.

text = 'ItsMyCode - Learn how to Code in Python '

# find the index of first occurence of substring
output = text.index('Hello')
print("The index of first occurrence of 'Hello' is:", output)

Output

Traceback (most recent call last):
  File "C:\Personal\IJS\Code\main.py", line 4, in <module>
    output = text.index('Hello')
ValueError: substring not found

Example 3: Python String index() using start and end arguments

text = 'ItsMyCode - Learn how to Code in Python '

# find the index of first occurence of substring 'Code'
output = text.index('Code', 18)
print("The index of first occurrence of 'Hello' is:", output)

# find the index of character '-'
output = text.index('-', 8, 12)
print("The index of first occurrence of '-' is:", output)

# find the index of substring 'to'
output = text.index('to', 18, -6)
print("The index of first occurrence of 'to' is:", output)

Output

The index of first occurrence of 'Hello' is: 25
The index of first occurrence of '-' is: 10
The index of first occurrence of 'to' is: 22
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python String Isidentifier()

Python String isidentifier()

Table of Contents Hide isidentifier() Syntaxisidentifier() Parametersisidentifier() Return ValueExample 1: How isidentifier() works? Example 1: How to use isidentifier() method? The Python String isidentifier() method is a built-in function that…
View Post
Python String Capitalize()

Python String capitalize()

Table of Contents Hide capitalize() Syntaxcapitalize() ParameterReturn Value from capitalize()Example: Capitalize a string in Python Python string capitalize() method will convert the first letter in a string to uppercase and keep…
View Post
Python List Insert()

Python List insert()

Table of Contents Hide Syntax of List insert() insert() ParametersReturn Value from List insert()Example 1: Inserting an Element to the ListExample 2: Inserting an element at the start and end of…
View Post
Python String Zfill()

Python String zfill()

Table of Contents Hide zfill() Syntaxzfill() Parameterszfill() Return ValueExample 1: How zfill() works in Python?Example 2: zfill() in case of Sign Prefix The Python String zfill() method is a built-in…
View Post