
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to check if a substring is contained in another string in Python
Python has a keyword 'in' for finding if a string is a substring of another string. For example
print('ello' in 'hello world')
OUTPUT
True
If you also need the first index of the substring, you can use find(substr) to find the index. If this method returns -1, it means that substring doesn't exist in the string. For example,
print("hello world".find('ello'))
OUTPUT
1
Checking if ‘no’ is contained in the string ‘Harry Potter: The Goblet of Fire'
print("Harry Potter: The Goblet of Fire".find('no'))
OUTPUT
-1
- Related Articles
- Python - Check if a list is contained in another list
- How to check if string or a substring of string starts with substring in Python?
- Check if substring present in string in Python
- Python Program to check if a substring is present in a given string.
- Check if a string is suffix of another in Python
- How to check if string or a substring of string ends with suffix in Python?
- How to check if a string contains a substring in Golang?
- How to check if a string is a subset of another string in R?
- How to check if multiple strings exist in another string in Python?
- Check if a string can be repeated to make another string in Python
- FabricJS – Check if a Polygon Object is Fully Contained within Another Object?
- Check if a String Contains a Substring in Linux
- Java program to check if a Substring is present in a given String
- C# program to check if a Substring is present in a Given String
- Check if it is possible to transform one string to another in Python

Advertisements