arno
arno

Reputation: 505

Test if one OR an other substring is in a string, in a DRY way

I'm a newbie in python, and try to grab the 'spirit' of it.
Simple question :
I want to test if either 'a' or 'b' are in a string 'xxxxxbxxxxx'
I obvisouly could do

full_string = 'xxxxxbxxxxx'
if 'a' in full_string or 'b' in full_string :  
    print 'found'

but I feel there is a more simple way to do it "python style", without repeating full_string , what could be it ?

Upvotes: 2

Views: 1142

Answers (1)

Eric
Eric

Reputation: 97555

I think this is as close as you can get:

full_string = 'xxxxxbxxxxx'
if any(s in full_string for s in ('a', 'b')):  
    print 'found'

Or you could use regular expressions:

import re

full_string = 'xxxxxbxxxxx'
if re.search('a|b', full_string):  
    print 'found'

Upvotes: 4

Related Questions