2012-10-20 23 views
2

我是一个Python新手,并试图抓住它的'精神'。
简单问题:
我想测试如果任'a''b'是在一个字符串'xxxxxbxxxxx'
我obvisouly能做测试一个或其他子字符串是否在一个字符串中,以干的方式

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

,但我觉得还有一个更简单的方法来做到这一点“蟒式” ,没有重复full_string,可能是什么?

回答

4

我认为这是接近你可以得到:

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

或者你可以使用正则表达式:

import re 

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

真棒,感谢埃里克。我爱蟒蛇。 – arno

相关问题