The problem isn't your use of not
, it's that or
doesn't mean what you think it does [and if you think it through, it couldn't]:
if not ["StatusRequest" or "StatusResponse"] in line:
You're asking whether the expression ["StatusRequest" or "StatusResponse"]
appears in line
. But that expression is just the same thing as "StatusRequest"
.
Put it in English: you're not trying to say "if neither of these is in line". Python doesn't have a neither
/none
function, but it does have an any
function, so you can do
this:
if not any[value in line for value in ["StatusRequest", "StatusResponse"]]:
This isn't quite as nice as English; in English, you can just say "if none of the values 'StatusRequest' and 'StatusResponse' are in line", but in Python, you have to say "if none of the values coming up are in line, for values 'StatusRequest' and 'StatusResponse'".
Or, maybe more simply in this case:
if "StatusRequest" not in line and "StatusResponse" not in line:
[Also, notice that you can use not in
, instead of using in
and then negating the whole thing.]
You are here: Home / Python / Check if String Does Not Contain Substring in Python
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.
Other Articles You'll Also Like:
- 1. Using Python to Remove Duplicates from List
- 2. Using pandas resample[] to Resample Time Series Data
- 3. Convert String to List Using Python
- 4. Generate Random Float Between 0 and 1 Using Python
- 5. How to Rotate a List in Python
- 6. Python issuperset[] Function – Check if Set is Superset of Another Set
- 7. Pandas Crosstab on Multiple Columns
- 8. Repeat String with * Operator in Python
- 9. Using Python to Print Degree Symbol
- 10. Python Get First Word in String
About The Programming Expert
The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.
Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.
At the end of the day, we want to be able to just push a button and let the code do it’s magic.
You can read more about us on our about page.
Reader Interactions
String manipulation is a common task in any programming language. Python provides two common ways to check if a string contains another string.
Python check if string contains another string
Python string supports in operator. So we can use it to check if a string is part of another string or not. The in operator syntax is:
sub in str
It returns True
if “sub” string is part of “str”,
otherwise it returns False
. Let’s look at some examples of using in
operator in Python.
str1 = 'I love Python Programming'
str2 = 'Python'
str3 = 'Java'
print[f'"{str1}" contains "{str2}" = {str2 in str1}']
print[f'"{str1}" contains "{str2.lower[]}" = {str2.lower[] in str1}']
print[f'"{str1}" contains "{str3}" = {str3 in str1}']
if str2 in str1:
print[f'"{str1}" contains "{str2}"']
else:
print[f'"{str1}" does not contain "{str2}"']
Output:
"I love Python Programming" contains "Python" = True
"I love Python Programming" contains "python" = False
"I love Python Programming" contains "Java" = False
"I love Python Programming" contains "Python"
If you are not familiar with f-prefixed strings in Python, it’s a new way for string formatting introduced in Python 3.6. You can read more about it at f-strings in Python.
When we use in operator, internally it calls __contains__[] function. We can use this function directly too, however it’s recommended to use in operator for readability purposes.
s = 'abc'
print['s contains a =', s.__contains__['a']]
print['s contains A =', s.__contains__['A']]
print['s contains X =', s.__contains__['X']]
Output:
s contains a = True
s contains A = False
s contains X = False
Using find[] to check if a string contains another substring
We can also use string find[] function to check if string contains a substring or not. This function returns the first index position where substring is found, else returns -1.
str1 = 'I love Python Programming'
str2 = 'Python'
str3 = 'Java'
index = str1.find[str2]
if index != -1:
print[f'"{str1}" contains "{str2}"']
else:
print[f'"{str1}" does not contain "{str2}"']
index = str1.find[str3]
if index != -1:
print[f'"{str1}" contains "{str3}"']
else:
print[f'"{str1}" does not contain "{str3}"']
Output:
"I love Python Programming" contains "Python"
"I love Python Programming" does not contain "Java"
You can checkout complete python script and more Python examples from our GitHub Repository.
Want to learn more? Join the DigitalOcean Community!
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.
Sign up