0

I want to check if a string the user inputted contains one of the strings in a list.

I know how to check if the string contains one string but I want to know how to check if a string contains either this string, or that string, or another string.

For example, if I am checking the user's input to see if it contains a vowel.. there can be 5 different choices when we're talking about vowels (a, e, i, o, or u). How can I check to see if the string the user inputted has one of those?

5 Answers 5

3

Using any:

>>> vowels = 'a', 'e', 'i', 'o', 'u'

>>> s = 'cat'
>>> any(ch in vowels for ch in s)
True
>>> s = 'pyth-n'
>>> any(ch in vowels for ch in s)
False
Sign up to request clarification or add additional context in comments.

4 Comments

Why choose any() over .intersection()?
@JasonDecastro, That only works for single character strings.
Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'any' is not defined <- what's wrong in here, python 2.4.3
@jester112358, any is available from Python 2.5.
2
>>> vowels = set(['a', 'e', 'i', 'o', 'u'])
>>> inp = "foobar"
>>> bool(vowels.intersection(inp))
True
>>> bool(vowels.intersection('qwty'))
False

2 Comments

This works, but there was another answer that showed any(), which also worked. Could you tell me why I should use .intersection() over any()?
@JasonDecastro For your example any is better, sets can be useful when the size of the set vowels is huge, in that case you can both any and sets together to get an efficient solution as sets provide O(1) lookup. BTW set.intersection returns a new set containing the unique common elements between vowels and inp, bool() on it returns True when the length of the returned set is > 0. And any just return True/False.
1

This is the basic logic you want:

def string_checker(user_input_string, vowels):
    for i in vowels:
       if i in user_input_string:
          return 'Found it'
    return 'Sorry grashooper :('

print string_checker('Hello',('a','e','i','o','u'))

The short cut way to do this is by using the built-in any() method; which will return true if any of the items in it returns a truth value.

Here is how it would work:

any(i in user_input_string for i in vowels)

The difference is that instead of returning a custom string, this method will return True or False; we can use it the same way in our loop:

if any(i in user_input_string for i in vowels):
    print('Found it!')
else:
    print('Oh noes! No vowels for you!')

1 Comment

This does work too, but so does .intersection(). Why should I choose any() over .intersection()?
0

Checking the strings sequentially works, but if it's too inefficient for your case, you can use Aho-Corasick algorithm.

Comments

-1

You can use this syntax

if myItem in list:
# do something

also look at the functions:

http://docs.python.org/2/library/functions.html

1 Comment

could you specify a reason why -1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.