Python 检查空格是否在字符串中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3301395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if space is in a string
提问by Marijus
' ' in word == True
I'm writing a program that checks whether the string is a single word. Why doesn't this work and is there any better way to check if a string has no spaces/is a single word..
我正在编写一个程序来检查字符串是否是单个单词。为什么这不起作用,有没有更好的方法来检查字符串是否没有空格/是单个单词..
采纳答案by Rob Lourens
==takes precedence over in, so you're actually testing word == True.
==优先于in,因此您实际上是在测试word == True.
>>> w = 'ab c'
>>> ' ' in w == True
1: False
>>> (' ' in w) == True
2: True
But you don't need == Trueat all. ifrequires [something that evalutes to True or False] and ' ' in wordwill evalute to true or false. So, if ' ' in word: ...is just fine:
但你根本不需要== True。if需要[' ' in word评估为真或假的东西],并将评估为真或假。所以,if ' ' in word: ...就好了:
>>> ' ' in w
3: True
回答by Wayne Werner
word = ' '
while True:
if ' ' in word:
word = raw_input("Please enter a single word: ")
else:
print "Thanks"
break
This is more idiomatic python - comparison against True or False is not necessary - just use the value returned by the expression ' ' in word.
这是更惯用的 python - 不需要与 True 或 False 进行比较 - 只需使用表达式返回的值' ' in word。
Also, you don't need to use pastebin for such a small snippet of code - just copy the code into your post and use the little 1s and 0s button to make your code look like code.
此外,对于这么小的代码片段,您不需要使用 pastebin - 只需将代码复制到您的帖子中,然后使用小 1 和 0 按钮使您的代码看起来像代码。
回答by Justin Ethier
You can say word.strip(" ")to remove any leading/trailing spaces from the string - you should do that before your ifstatement. That way if someone enters input such as " test "your program will still work.
您可以说word.strip(" ")从字符串中删除任何前导/尾随空格 - 您应该在您的if语句之前这样做。这样,如果有人输入诸如" test "您的程序之类的输入仍然可以工作。
That said, if " " in word:will determine if a string contains any spaces. If that does not working, can you please provide more information?
也就是说,if " " in word:将确定字符串是否包含任何空格。如果这不起作用,您能否提供更多信息?
回答by Jukka Suomela
Write if " " in word:instead of if " " in word == True:.
写if " " in word:而不是if " " in word == True:.
Explanation:
解释:
- In Python, for example
a < b < cis equivalent to(a < b) and (b < c). - The same holds for any chain of comparison operators, which include
in! - Therefore
' ' in w == Trueis equivalent to(' ' in w) and (w == True)which is notwhat you want.
- 例如,在 Python
a < b < c中相当于(a < b) and (b < c). - 这同样适用于任何比较运算符链,包括
in! - 因此
' ' in w == True相当于(' ' in w) and (w == True)哪个不是你想要的。
回答by Guillaume Lebourgeois
There are a lot of ways to do that :
有很多方法可以做到这一点:
t = s.split(" ")
if len(t) > 1:
print "several tokens"
To be sure it matches every kind of space, you can use re module :
为了确保它匹配各种空间,您可以使用 re 模块:
import re
if re.search(r"\s", your_string):
print "several words"
回答by John Howard
Use this:
用这个:
word = raw_input("Please enter a single word : ")
while True:
if " " in word:
word = raw_input("Please enter a single word : ")
else:
print "Thanks"
break
回答by Teodor Pripoae
You can try this, and if it will find any space it will return the position where the first space is.
你可以试试这个,如果它找到任何空格,它会返回第一个空格所在的位置。
if mystring.find(' ') != -1:
print True
else:
print False
回答by Nalaka - CS Teacher at SLSM
# The following would be a very simple solution.
print("")
string = input("Enter your string :")
noofspacesinstring = 0
for counter in string:
if counter == " ":
noofspacesinstring += 1
if noofspacesinstring == 0:
message = "Your string is a single word"
else:
message = "Your string is not a single word"
print("")
print(message)
print("")
回答by FlipperCanoe163
You can use the 're' module in Python 3.
If you indeed do, use this:
您可以在 Python 3 中使用“re”模块。
如果确实如此,请使用以下命令:
re.search('\s', word)
This should return either 'true' if there's a match, or 'false' if there isn't any.
如果有匹配项,则应返回“true”,如果不匹配,则应返回“false”。

