In Python, we can easily check if a string does not contains a substring using the in operator and not operator.
string = "Hello"
if "z" not in string:
print("z not in string")
else:
print("z in string")
#Output:
z not in string
When working with strings, it can be useful to know if a substring is contained in a string variable.
You can check if a string does not contain easily in Python.
To check if a string does not contain a particular substring, you can use the in operator and not operator.
Below is a simple example showing you how to check if a string does not contain another string in Python.
string = "Hello"
if "z" not in string:
print("z not in string")
else:
print("z in string")
#Output:
z not in string
Checking to See if a String Doesn’t Have Vowels in Python
You can check if a string doesn’t contain any vowels easily in Python.
To do so, you can use a loop and check if any vowel is contained in the string.
If none of the vowels are in the string, then you can conclude there are no vowels in the string.
Below is a simple example of how to check if a string has no vowels in Python.
string = "ccctttrrry"
def doesNotContainsVowels(s):
string = string.lower()
contains = False
for char in string:
if char in "aeiou":
contains = True
return contains
print(doesNotContainVowels("ccccttttwwx"))
print(doesNotContainVowels("a"))
#Output:
False
True
Hopefully this article has been useful for you to check if a string does not contain another string using Python.