繁体   English   中英

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

[英]Determine whether there’s more than one word in a string

例如:

line = 'how are you?'

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

这可能吗?

尝试这个:

line = 'how are you?'

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

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

if ' ' in 'how are you?': line.split()
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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM