substring
A substring is a run of characters that appears inside a larger string, contiguous and in order. In Python, a substring of a str is itself a str, so "Py", "tho", and "Python" all count as substrings of "Python". So does the empty string, which is why "" in text is always True.
Contiguity is what separates a substring from a subsequence. Reading "Pto" out of "Python" keeps the characters in order but skips others in between, so it’s a subsequence rather than a substring.
Click the characters below to pick a run of your own, and watch what happens the moment you skip one:
You get a substring by slicing the original with a start and stop index, as in text[0:2]. Because strings are immutable, the slice is a brand-new string and the original stays untouched.
To ask whether a substring is present, use the in operator. When you need its position, str.find() returns the lowest index where the substring starts, or -1 when it’s absent, and str.index() raises a ValueError in that case instead. str.count() tallies non-overlapping occurrences, while str.startswith() and str.endswith() check whether the string begins or ends with a given substring.
Example
Say you’re scanning a log line to confirm that it recorded an error and to pull out the request ID it carries:
>>> line = "2026-09-07 ERROR request_id=7f3a timed out"
>>> "ERROR" in line
True
>>> start = line.index("request_id=") + len("request_id=")
>>> line[start:start + 4]
'7f3a'
The in check answers the yes-or-no question without caring where the match sits. Then .index() reports where "request_id=" begins, and adding its length moves you past the label. Slicing four characters from there hands back '7f3a', a new string that’s a substring of line.
Related Resources
Tutorial
How to Check if a Python String Contains a Substring
In this tutorial, you'll learn the best way to check whether a Python string contains a substring. You'll also learn about idiomatic ways to inspect the substring further, match substrings with conditions using regular expressions, and search for substrings in pandas.
For additional information on related topics, take a look at the following resources:
- Strings and Character Data in Python (Tutorial)
- Check if a Python String Contains a Substring (Course)
- Reverse Strings in Python: reversed(), Slicing, and More (Tutorial)
- How to Split a String in Python (Tutorial)
- How to Replace a String in Python (Tutorial)
- Efficient String Concatenation in Python (Tutorial)
- How to Check if a Python String Contains a Substring (Quiz)
- Strings and Character Data in Python (Course)
- Python Strings and Character Data (Quiz)
- Reversing Strings in Python (Course)
- Python String Splitting (Course)
- How to Split a String in Python (Quiz)
- Replacing a String in Python (Course)
- Concatenating Strings in Python Efficiently (Course)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Martin Breuss • Updated Oct. 5, 2026