Use the in operator:

if "blah" not in somestring: 
    continue

Note: This is case-sensitive.

Answer from Michael Mrozek on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › check-if-string-contains-substring-in-python
Check if String Contains Substring in Python - GeeksforGeeks
December 20, 2025 - ... s = "Geeks welcome to the Geek ... found! Substring not found! ... The operator.contains() function checks for substring presence in a string programmatically....
Discussions

Is there a way to check if a string is a number?
value = input() try: float(value) except ValueError: #somethin else: #something More on reddit.com
🌐 r/learnpython
36
30
September 8, 2024
is string.find(substring) time complexity of O(n) or (O*m)?
It doesn't seem to be specified anywhere but I would expect it to be O(n×m) where n is the length of string and m is the length of substring. More on reddit.com
🌐 r/cpp_questions
5
4
May 14, 2019
Why is there no "dotted dict"?
From my point of view, dot notation is for objects, and indexing is for data structures. Using dot notation to access data inside dicts means there will be confusion between actual data and methods/internals (remember, Python doesn't have external visibility controls) and that disallows you to have keys like 'update' (because that's a method and accessing it will be ambiguous) and gives you a thousand more ways to shoot yourself in the foot. JavaScript made the same mistake with objects vs. dicts and backfired for them. More on reddit.com
🌐 r/Python
41
1
August 29, 2014
How to check if a line in Bash has a newline

A trailing newline specifically? Use:

[[ $line == *$'\n' ]]

A newline anywhere in the string? Use:

[[ $line == *$'\n'* ]]

$'...' is a form of quoting in Bash that understands C-style escapes.

More on reddit.com
🌐 r/bash
13
15
April 30, 2021
🌐
Reddit
reddit.com › r/python › honest question: why don't python strings have a "contains" method?
r/Python on Reddit: Honest Question: Why don't Python strings have a "contains" method?
September 6, 2022 -

I know there is the in keyword which does the job and does it well, but it's a question of consistency. Isn't one of those python zens all about consistency?

When I can do str.startswith('Foo') and also str.endswith('Foo'), then str.contains('Foo') should be quite obvious and intuitive isn't it? Instead of that, I have to do this:

if 'Foo' in str:
    do_something()

While this does the job and does it great, the contains() method is more practical and intuitive, isn't it? And it's not that the core team even has to do a lot of effort for that. We already have str.__contains__('Foo') which works, so all they have to do is to turn it into a proper method!

🌐
Codecademy
codecademy.com › article › how-to-check-if-a-string-contains-a-substring-in-python
How to Check if a String Contains a Substring in Python | Codecademy
For example, when working with ... as functions rather than operators. This method takes a string and a substring as arguments and searches for the substring in the main string....
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.contains.html
pandas.Series.str.contains — pandas 3.0.6 documentation
>>> s1.str.contains("oG", case=True, regex=True) 0 False 1 False 2 False 3 False 4 False dtype: bool · Returning ‘house’ or ‘dog’ when either expression occurs in a string.
🌐
W3Schools
w3schools.com › python › gloss_python_check_string.asp
Python Check In String
To check if a certain phrase or character is present in a string, we can use the keywords in or not in. Check if the phrase "ain" is present in the following text: txt = "The rain in Spain stays mainly in the plain" x = "ain" in txt print(x) Try it Yourself » · Check if the phrase "ain" is NOT present in the following text: txt = "The rain in Spain stays mainly in the plain" x = "ain" not in txt print(x) Try it Yourself » · Python Strings Tutorial String Literals Assigning a String to a Variable Multiline Strings Strings are Arrays Slicing a String Negative Indexing on a String String Length Format String Escape Characters
Find elsewhere
🌐
Real Python
realpython.com › python-string-contains-substring
How to Check if a Python String Contains a Substring – Real Python
September 10, 2025 - The .str.contains() method in pandas identifies which DataFrame entries contain a specific substring. Understanding these methods and tools enables you to effectively check for substrings in Python strings, catering to various needs from simple ...
🌐
Python Morsels
pythonmorsels.com › substring-check
Substrings in Python: checking if a string contains another string - Python Morsels
July 18, 2026 - What if you need to check whether one string contains another specifically at the beginning or the end of the string? You can use the string startswith method to check whether one string starts with another string: >>> whole = "I enjoy writing Python code." >>> whole.startswith("You ") False >>> whole.startswith("I ") True
🌐
LearnDataSci
learndatasci.com › solutions › python-string-contains
Python String Contains – See if String Contains a Substring – LearnDataSci
The easiest and most effective way to see if a string contains a substring is by using if ... in statements, which return True if the substring is detected. Alternatively, by using the find() function, it's possible to get the index that a substring starts at, or -1 if Python can't find the ...
🌐
Mimo
mimo.org › glossary › python › string-find
Python string.find(): Syntax, Usage, and Examples
Start your coding journey with Python. Learn basics, data types, control flow, and more ... The method specifically returns the index of the first occurrence of the substring, making it different from other search methods in languages like Java or JavaScript. The .find() method is a built-in string method used to locate the starting index of the first occurrence of a substring within another string.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-check-if-string-contains-another-string
How To Check If a String Contains Another String in Python | DigitalOcean
Learn how to check if one string contains another in Python using in, find(), and regular expressions. Explore common pitfalls, and efficient practices.
🌐
Bacancy Technology
bacancytechnology.com › qanda › python › python-string-contains
How does Python string contains substring method?
However, there are a few ways to check if a string contains a substring. One way is to use the in operator. The in operator returns True if the substring is found in the string, and False if it is not found.
🌐
Sentry
sentry.io › sentry answers › python › test whether a string contains a substring in python
Python Contains String: Check for Substring Matches | Sentry
July 3, 2026 - if "This string contains word.".__contains__("word"): print("Found 'word' in string!") However, this is unpythonic, as methods starting with underscores are considered private. Calling them externally should be avoided where possible, even though Python does not prevent you from doing so.
🌐
YouTube
youtube.com › case digital
Check If A String Contains A Substring In Python - YouTube
In this python tutorial, we discuss the different ways on how to check if a string contains a substring in python! In this video, we break down 6 different m...
Published: May 29, 2021
Views: 6K
🌐
ReqBin
reqbin.com › code › python › wzo2s8ib › python-string-contains-example
How to check if a string contains a substring in Python?
December 20, 2022 - To check if a Python string contains the desired substring, you can use the "in" operator or the string.find() method. To get the first index of a substring in a string, you can use the string.index(). To count the number of substrings in a ...
🌐
O'Reilly
oreilly.com › library › view › python-cookbook › 0596001673 › ch03s07.html
Checking Whether a String Contains a Set of Characters - Python Cookbook [Book]
July 19, 2002 - The solution generalizes to any sequence (not just a string), and any set (any object in which membership can be tested with the in operator, not just one of characters): def containsAny(str, set): """ Check whether sequence str contains ANY of the items in set.
Authors: Alex MartelliDavid Ascher
Published: 2002
Pages: 608
🌐
Python
docs.python.org › 3 › library › string.html
string — Common string operations
Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output.
🌐
Stack Abuse
stackabuse.com › python-check-if-string-contains-substring
Python: Check if String Contains Substring
June 20, 2023 - The easiest way to check if a Python string contains a substring is to use the in operator.
🌐
Python
docs.python.org › 3 › builtins › stdtypes.html
Built-in Types — Python 3.14.7 documentation
The string on which this method is called can contain literal text or replacement fields delimited by braces {}. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument.