确定字符串中是否有多个单词

时间:2014-12-03 20:14:51

标签: python string split

例如:

line = 'how are you?'

if line == [has more than one word]: line.split()

这可能吗?

3 个答案:

答案 0 :(得分:8)

试试这个:

line = 'how are you?'

if len(line.split()) > 1: # has more than 1 word

答案 1 :(得分:2)

获取line.split()似乎更加pythonic,但如果这将是一个昂贵的操作,这可能会更快:

if ' ' in 'how are you?': line.split()

答案 2 :(得分:2)

line.strip().count(' ') > 0 # finds at least one separator after removing the spaces at the left and the right of the actual string

或者如果你真的想要速度

line.strip().find(' ') != -1 # not finding the ' ' character in the string
相关问题