首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查字符串是否仅为字母和空格- Python

检查字符串是否仅为字母和空格- Python
EN

Stack Overflow用户
提问于 2015-04-05 17:55:27
回答 9查看 41.3K关注 0票数 13

试图让python返回字符串只包含字母和空格。

代码语言:javascript
复制
string = input("Enter a string: ")

if all(x.isalpha() and x.isspace() for x in string):
    print("Only alphabetical letters and spaces: yes")
else:
    print("Only alphabetical letters and spaces: no")

我一直在尝试please,作为Only alphabetical letters and spaces: no,我使用了or而不是and,但这只满足了一个条件。我需要满足这两个条件。也就是说,句子必须只包含字母和空格,但必须至少有一种类型。它不能包含任何数字。

对于python返回字母和空格只包含在字符串中,我在这里遗漏了什么?

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2015-04-05 17:57:51

字符不能同时是alpha (空格)。它可以是一个alpha 一个空格。

要求字符串仅包含alpha和空格:

代码语言:javascript
复制
string = input("Enter a string: ")

if all(x.isalpha() or x.isspace() for x in string):
    print("Only alphabetical letters and spaces: yes")
else:
    print("Only alphabetical letters and spaces: no")

要求字符串至少包含一个alpha和至少一个空格:

代码语言:javascript
复制
if any(x.isalpha() for x in string) and any(x.isspace() for x in string):

若要要求字符串至少包含一个alpha、至少一个空格,并且只包含alpha和空格,请执行以下操作:

代码语言:javascript
复制
if (any(x.isalpha() for x in string)
    and any(x.isspace() for x in string)
    and all(x.isalpha() or x.isspace() for x in string)):

测试:

代码语言:javascript
复制
>>> string = "PLEASE"
>>> if (any(x.isalpha() for x in string)
...     and any(x.isspace() for x in string)
...     and all(x.isalpha() or x.isspace() for x in string)):
...     print "match"
... else:
...     print "no match"
... 
no match
>>> string = "PLEASE "
>>> if (any(x.isalpha() for x in string)
...     and any(x.isspace() for x in string)
...     and all(x.isalpha() or x.isspace() for x in string)):
...     print "match"
... else:
...     print "no match"
... 
match
票数 23
EN

Stack Overflow用户

发布于 2015-04-05 18:01:02

正确的解决方案将使用or

代码语言:javascript
复制
string = input("Enter a string: ")

if all(x.isalpha() or x.isspace() for x in string):
    print("Only alphabetical letters and spaces: yes")
else:
    print("Only alphabetical letters and spaces: no")

虽然您有一个字符串,但您正在迭代该字符串的字母,因此每次只有一个字母。因此,一个字符本身不可能是一个字母字符和一个空格,但它只需要是其中之一,以满足您的约束。

编辑:我在另一个回答中看到了你的评论。alphabet = string.isalpha()返回True,当且仅当字符串中的所有字符都是字母。这不是您想要的,因为您说希望您的代码在使用字符串yes执行时打印please,该字符串有一个空格。您需要单独检查每个字母,而不是整个字符串。

为了让您确信代码确实是正确的(好的,您需要自己执行它才能被说服,但无论如何):

代码语言:javascript
复制
>>> string = "please "
>>> if all(x.isalpha() or x.isspace() for x in string):
    print("Only alphabetical letters and spaces: yes")
else:
    print("Only alphabetical letters and spaces: no")


Only alphabetical letters and spaces: yes

编辑2:从您的新评论来看,需要这样的内容:

代码语言:javascript
复制
def hasSpaceAndAlpha(string):
    return any(char.isalpha() for char in string) and any(char.isspace() for char in string) and all(char.isalpha() or char.isspace() for char in string)

>>> hasSpaceAndAlpha("text# ")
False
>>> hasSpaceAndAlpha("text")
False
>>> hasSpaceAndAlpha("text ")
True

代码语言:javascript
复制
def hasSpaceAndAlpha(string):
    if any(char.isalpha() for char in string) and any(char.isspace() for char in string) and all(char.isalpha() or char.isspace() for char in string):
        print("Only alphabetical letters and spaces: yes")
    else:
        print("Only alphabetical letters and spaces: no")

>>> hasSpaceAndAlpha("text# ")
Only alphabetical letters and spaces: no
>>> hasSpaceAndAlpha("text")
Only alphabetical letters and spaces: no
>>> hasSpaceAndAlpha("text ")
Only alphabetical letters and spaces: yes
票数 3
EN

Stack Overflow用户

发布于 2015-04-05 18:55:04

实际上,这是一个模式匹配练习,那么为什么不使用模式匹配呢?

代码语言:javascript
复制
import re
r = re.compile("^[a-zA-Z ]*$")
def test(s):
    return not r.match(s) is None  

或者在解决方案中是否需要使用any()

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29460405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档