How to Get Index of a Substring in Python?
Last Updated :
20 Nov, 2024
To get index of a substring within a Python string can be done using several methods such as str.find(), str.index(), and even regular expressions. Each approach has its own use case depending on the requirements. Let’s explore how to efficiently get the index of a substring.
The simplest way to get the index of a substring in a string is by using the find() method. This method returns the first index where the substring is found or -1 if the substring does not exist.
Python
s1 = "Welcome to Python programming"
s2 = "Python"
# Using find() method
sub = s1.find(s2)
print(sub)
Output:
11
find() method searches for the substring "Python" in the string and returns the index of its first occurrence. If the substring is not found, it returns -1.
Let's Explore other methods of finding index of a substring in python:
Using str.index()
Another way to find the index of a substring is by using the index() method. Unlike find(), it raises a ValueError if the substring is not found, making it useful for cases where you want to ensure the substring exists.
Python
# Using index() method
s1 = "Welcome to Python programming"
s2 = "Python"
# using try..except to handle exception,
# in case substring is not found
try:
sub = s1.index(s2)
print(sub)
except ValueError:
print("Substring not found")
index() method returns index of first occurrence of the substring "Python". If the substring is not present, it raises a ValueError.
Using Regular Expressions
For more complex matching scenarios, you can use regular expressions with the re
module. This allows for pattern-based searches within strings.
Python
import re
s1 = "Welcome to Python programming"
s2 = "Python"
# Using regex to find the substring
match = re.search(s2, s1)
if match:
print("Found at index:", match.start())
else:
print("Substring not found")
re.search() method searches for the first occurrence of the pattern and returns a match object. The match.start() method returns the starting index of the match.
Similar Reads
How to Find Index of a Substring in Python In Python, sometimes we need to know where a certain substring appears in a string. To do this, we can find the index (or position) of that substring. The index tells us where the substring is located. Letâs look at some simple ways to find the index of a substring in a string.Using find() methodThe
2 min read
Find the Index of a Substring in Python Finding the position of a substring within a string is a common task in Python. In this article, we will explore some simple and commonly used methods to find the index of a substring in Python.Using str.find() The find() method searches for the first occurrence of the specified substring and return
3 min read
How to Remove a Substring in Python? In Python, removing a substring from a string can be achieved through various methods such as using replace() function, slicing, or regular expressions. Depending on your specific use case, you may want to remove all instances of a substring or just the first occurrence. Letâs explore different ways
2 min read
Python | Get first index values in tuple of strings Yet another peculiar problem which might not be common, but can occur in python programming while playing with tuples. Since tuples are immutable, they are difficult to manipulate and hence knowledge of possible variation solutions always helps. This articles solves problem of extracting only the fi
5 min read
Python - Word starting at Index Sometimes, while working with Python, we can have a problem in which we need to extract the word which is starting from a particular index. This can have a lot of applications in school programming. Let's discuss certain ways in which this task can be performed. Method #1: Using a loop This is a bru
2 min read