Use the in operator:
if "blah" not in somestring:
continue
Note: This is case-sensitive.
Answer from Michael Mrozek on Stack Overflow Top answer 1 of 10
8686
Use the in operator:
if "blah" not in somestring:
continue
Note: This is case-sensitive.
2 of 10
949
You can use str.find:
s = "This be a string"
if s.find("is") == -1:
print("Not found")
else:
print("Found")
The
find()method should be used only if you need to know the position of sub. To check if sub is a substring or not, use theinoperator. (c) Python reference
How to check if a string contains a character in a list
Use a loop , iterate through the list, check for existence of the character . More on reddit.com
Is there a way to check if a string is a number?
Nobody mentioned str.isnumeric()... More on reddit.com
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
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
How to Find in String using Python | String Contains Substring ...
17:54
Check If A String Contains A Substring In Python - YouTube
02:00
Check if a Python String Contains a Substring (Overview) (Video) ...
05:22
How To Check If A String Contains Numbers Or Text In Python. 2024 ...
03:59
How to Check if a Python String Contains Numbers - YouTube
Real Python
realpython.com › python-string-contains-substring
How to Check if a Python String Contains a Substring – Real Python
September 10, 2025 - Python counted how often the substring appears in the string and returned the answer. The text contains the substring four times. But what do these substrings look like? You can inspect all the substrings by splitting your text at default word borders and printing the words to your terminal using a for loop: ... >>> for word in file_content.split(): ... if "secret" in word: ... print(word) ... secret secret. secret, secretly · In this example, you use .split() to separate the text at whitespaces into strings, which Python packs into a list.
AskPython
askpython.com › python › string › python-string-contains
Python String contains: Check if String Contains Substring - AskPython
April 27, 2023 - The ‘in’ operator, find(), and index() methods are frequently used to check for a substring within a string by developers and we have attached the link to our tutorial on each above-mentioned string method so that you can read them too, but for this tutorial, we will be using the built-in function __contains__(). The Python string __contains__() function can be used with a string object, it takes a substring as an argument and returns True if the substring is present in the string.
CodeConverter
codeconverter.com › articles › python-string-contains
Python String Contains — Every Way to Check (Ranked) | CodeConverter Blog
February 12, 2026 - A return value of 0 means "not found," so you could technically use .count() > 0 as a contains check. But that's doing extra work — it scans the entire string counting occurrences when you only need to know if there's at least one.
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.contains.html
pandas.Series.str.contains — pandas 3.0.6 documentation
Same as startswith, but tests the end of string. ... Returning a Series of booleans using only a literal pattern. >>> s1 = pd.Series(["Mouse", "dog", "house and parrot", "23", np.nan]) >>> s1.str.contains("og", regex=False) 0 False 1 True 2 False 3 False 4 False dtype: bool
GeeksforGeeks
geeksforgeeks.org › python › check-if-string-contains-substring-in-python
Check if String Contains Substring in Python - GeeksforGeeks
December 20, 2025 - The in operator in Python checks if one string occurs within another. It evaluates to True if the substring is present in the main string, otherwise False. ... s = "Geeks welcome to the Geek Kingdom!" if "Geek" in s: print("Substring found!") else: print("Substring not found!") if "For" in s: print("Substring found!") else: print("Substring not found!") ... Substring found! Substring not found! ... The operator.contains() function checks for substring presence in a string programmatically.
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.
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 more detailed checks, like finding the position of the substring or avoiding exceptions, methods like .find(), .index(), and .contains() come into play. If you want to learn more about strings and string operations in Python, check out the Learn Python 3 course on Codecademy. Yes, the in operator is case-sensitive. For example, “Apple” in “Pineapple” returns False because of the capital “A”.
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 ...
Udemy
blog.udemy.com › home › it & development › software development › the many ways to check if a python string contains a substring
Ways to Check if a Python String Contains a Substring - Udemy Blog
April 14, 2026 - What if the string could contain the substring more than once? Here is the syntax of the find method: We can use the two other arguments to set a starting and ending point in the string we are searching. Here is an example: The code above will return the start index of the second occurrence of the substring “sea.” · 100 Days of Code™: The Complete Python Pro Bootcamp Dr.
Java Code Geeks
examples.javacodegeeks.com › home › web development › python
Python String contains() method Tutorial - Java Code Geeks
March 2, 2022 - Download You can download the full source code of this example here: Python String contains() method Tutorial
DataCamp
datacamp.com › tutorial › python-string-contains
Python String Search and Replace: in, .find(), .index(), .count(), and .replace() | DataCamp
March 31, 2026 - Python’s text strings are Unicode. Substring operations are case-sensitive by default and implemented in C for speed. The most direct way to test if text appears inside a string is the in operator, which returns a Boolean using the string’s __contains__() implementation.
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
Scaler
scaler.com › home › topics › how to check if string contains substring in python?
How to Check if String Contains Substring in Python? - Scaler Topics
March 15, 2024 - Time Complexity: The time complexity is O(n + m), n = length of the main string, and m is the number of words in the split string. This method splits the string and then iterates over the words to perform the search. In this example, the variable substring contains Python as a substring and will check whether it is present in main_string using the split method.