Sometimes, we want to compare a string to multiple items in Python.
In this article, we’ll look at how to compare a string to multiple items in Python.
How to compare a string to multiple items in Python?
To compare a string to multiple items in Python, we can use the in operator.
For instance, we write:
accepted_strings = ['auth', 'authpriv', 'daemon']
facility = 'auth'
if facility in accepted_strings:
print('accepted')
We have the accepted_strings list.
Then we can use the in operator to check if facility included in the accepted_strings list.
Since this is True, 'accepted' is printed.
Conclusion
To compare a string to multiple items in Python, we can use the in operator.