Best to check string contains substring or not is using the in operator in Python. The in operator is generally used with if statement to check string contains a word or not.
99% of use cases will be covered using the keyword, in, which returns True or False:
if "blah" not in somestring: continue
Note: Does Python have a string ‘contains’ substring method
Python example Check if String Contains Substring
Simple python example code.
fullstring = "EyeHunt Python tutorial" substring = "Python" if substring in fullstring: print("Found!") else: print("Not found!")
If it’s just a substring search you can use
s = "This be a string" if s.find("is") == -1: print("No 'is' here!") else: print("Found 'is' in the string.")
Output: Found ‘is’ in the string.
Do comment if you have any doubts and suggestions on this Python condition statement topic.
Note: IDE: PyCharm 2021.1.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.