
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check Substring in Another String in Python
In Python, you can check whether a substring exists within another string using Python in operator, or string methods like find(), index(), and __contains__().
A string in Python is a sequence of characters that is enclosed in quotes. You can use either single quotes '...' or double quotes "..." to write a string, like this -
"Hello" //double quotes 'Python' //single quote
A substring in Python simply means a part of a string. For example,
text = "Python" part = "tho"
Here, "tho" is a substring of the string "Python".
Using the in operator
We can check if a substring exists inside another string by using the in keyword in Python. It returns True if the substring is found, and False if it is not.
Example
In this example, we are checking if the word "Python" exists in the string or not using the in operator -
text = "Python is fun" result = "Python" in text print(result)
The output is -
True
Using the find() method
The find() method in Python accepts a string value as a parameter and verifies for it in the current string. In case of a match, it gives you the position (starting index) of the first match.
If the substring is not found, this method returns -1. This is useful when you want to know the exact place where the substring starts.
Example
In this example, we are checking the presence of the substring "fun"in the string "Python is fun". We are also printing its position -
text = "Python is fun" index = text.find("fun") print(index)
The output obtained is -
10
Using the index() method
The index() method is similar to find(), it also accepts an index value as a parameter and returns the position of the given substring in the current string.
The only difference is unlike the find() method, if the substring doesn't exist, instead of returning -1, this function generates an error named ValueError.
Example
In this example, we are checking the position of the substring "is" in the string "Python is fun" using the index() method -
text = "Python is fun" position = text.index("is") print(position)
The result produced is -
7
Handling the exception of the index() Method
As the index() method generates an error if the given substring is not found, therefore, it is recommended to use this method within the try block (of try-except) to avoid the exception.
Example
In this example, we use the try-except block to catch the ValueError in case the substring doesn't exist -
text = "Python is fun" try: position = text.index("Java") except ValueError: print("Substring not found.")
Following is the output obtained -
Substring not found.
Using __contains__() method
When you use the in operator in Python to check if a substring exists inside a string, Python internally uses a method named __contains__(). You can also call this method explicitly to get the same result.
Example
In this example, we are directly calling the __contains__() method to check the presence of the substring in a string -
text = "Python is fun" print(text.__contains__("fun"))
We get the output as shown below -
True
Using Regular Expressions
You can also use Python's re module to find substrings, especially when you want to search for patterns (not just exact substrings). The re.search() function helps you do this easily.
Example
In this example, we use a regular expression to check if the substring "fun" appears in the given string -
import re text = "Python is fun" if re.search("fun", text): print("Found") else: print("Not found")
Following is the output of the above code -
Found