Home  >  Article  >  Backend Development  >  Python program to get index of substring in string

Python program to get index of substring in string

WBOY
WBOYforward
2023-08-27 12:17:06959browse

Python program to get index of substring in string

In Python, we can use the find() and index() methods to get the substring in a string for the first time The index that appears. Python provides various string manipulation functions to modify and access text data. In this article, we will write a program to get the index of a substring in a string.

Method 1: Use find() method

The find method searches for a specific substring passed as a parameter to the function and returns the starting index of the substring. If the substring is not found in the string, the find() method returns -1.

When calling the find() method, it accepts 3 parameters −

  • Substring - The substring you are searching for in the string

  • Start index (optional) - The index from which you want the find() method to start searching. Its default value is 0, which is the starting index of the string.

  • Ending index (optional) − The index at which you want the find() method to continue searching for the substring. Its default value is the length of the string, which is the end index of the string.

grammar

string.find(substring, start, end)

String is the string you want to find the substring index of. The starting and ending indices passed as arguments are optional. When not passed as a parameter, it takes the default value.

The Chinese translation of

Example

is:

Example

If we want to find the index of the substring "World" in the string "Hello, World!". We can use the find() method to achieve this, as shown below −

string = "Hello, World!"
substring = "World"
index = string.find(substring)
print(index)

Output

7

Method 2: Use index() method

The index() method returns the index of the first occurrence of the substring in the string. If the substring is not found, it raises a valueError.

Similar to the find() method, the index method also takes three parameters -

  • Substring - The substring you are searching for in the string

  • Start index (optional) − Which index do you want the find() method to start searching from. Its default value is 0, which is the starting index of the string.

  • Ending index (optional) − The index at which you want the find() method to continue searching for the substring. Its default value is the length of the string, which is the end index of the string.

grammar

string.index(substring, start, end)

String is the string you want to find the substring index of. The starting and ending indices passed as arguments are optional. When not passed as an argument, it takes the default value.

The Chinese translation of

Example

is:

Example

If we want to find the index of the substring "Point" in the string "Welcome to Tutorial Point", we can use the index() method, as follows -

string = "Welcome to Tutorial Point"
substring = "Point"
index = string.index(substring)
print(index)

Output

20

Handling errors

The index() method will raise ValueError when the substring is not found in the string, and the find() method will return -1. In order to handle the error in the index() method and display the output as "substring not found", we can use a try-except block to handle this error.

The Chinese translation of

Example

is:

Example

If we want to find the index of the substring "Python" in the string "Welcome to Tutorials Point", then the find() method will return -1 and the index() method will raise a value error because of the substring is not in the string. Here is how we can use try-except block to handle errors in index function.

string = "Welcome to Tutorials Point"
substring = "Python"
try:
   index = string.index(substring)
except ValueError:
   print("Substring not found")

Output

Substring not found

When the substring is not found in the string, the find() method will simply return -1. So in case of find() method we don't need to handle any error.

The Chinese translation of

Example

is:

Example

string = "Welcome to Tutorials Point"
substring = "Python"
index = string.find(substring)
print(index)

Output

-1

in conclusion

In Python, we can use the find() and index() methods to get the index of the first occurrence of a substring. Both methods take as arguments a substring, a starting index (optional), and an ending index (optional). The find() method returns -1 when the substring is not found, and the index() method raises a valueError when the substring is not found in the string. We can use try-except block in Python to handle valueError.

The above is the detailed content of Python program to get index of substring in string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete