Categories
Python Answers

How to compare a string to multiple items in Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *