检查字符串是否只是字母和空格 - Python

时间:2020-12-18 21:37:36

Trying to get python to return that a string contains ONLY alphabetic letters AND spaces

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

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")

I've been trying please and it comes up as Only alphabetical letters and spaces: no I've used or instead of and, but that only satisfies one condition. I need both conditions satisfied. That is the sentence must contain only letters and only spaces but must have at least one of each kind. It must not contain any numerals.

我一直在尝试,它出现只有字母的字母和空格:不,我已经使用或代替和,但只满足一个条件。我需要满足两个条件。这句话必须只包含字母和空格,但必须至少包含一种。它不得包含任何数字。

What am I missing here for python to return both letters and spaces are only contained in the string?

我在这里错过了什么让python返回字母和空格只包含在字符串中?

5 个解决方案

#1


13  

A character cannot be both an alpha and a space. It can be an alpha or a space.

角色不能同时是角色和空间。它可以是alpha或空格。

To require that the string contains only alphas and spaces:

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

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")

To require that the string contains at least one alpha and at least one space:

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

if any(x.isalpha() for x in string) and any(x.isspace() for x in string):

To require that the string contains at least one alpha, at least one space, and only alphas and spaces:

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

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)):

Test:

>>> 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

#2


3  

The correct solution would use an or.

正确的解决方案是使用或。

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")

Although you have a string, you are iterating over the letters of that string, so you have one letter at a time. So, a char alone cannot be an alphabetical character AND a space at the time, but it will just need to be one of the two to satisfy your constraint.

虽然你有一个字符串,但你正在遍历该字符串的字母,所以你一次只能有一个字母。因此,单独的char不能是字母字符和当时的空格,但它只需要是满足约束条件的两个中的一个。

EDIT: I saw your comment in the other answer. alphabet = string.isalpha() return True, if and only if all characters in a string are alphabetical letters. This is not what you want, because you stated that you want your code to print yes when execute with the string please, which has a space. You need to check each letter on its own, not the whole string.

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

Just to convince you that the code is indeed correct (well, ok, you need to execute it yourself to be convinced, but anyway):

只是为了让你相信代码确实是正确的(好吧,你需要自己执行才能确信,但无论如何):

>>> 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

EDIT 2: Judging from your new comments, you need something like this:

编辑2:从你的新评论来看,你需要这样的东西:

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

or

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


2  

Actually, it is an pattern matching exercise, so why not use pattern matching?

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

import re
r = re.compile("^[a-zA-Z ]*$")
def test(s):
    return not r.match(s) is None  

Or is there any requirement to use any() in the solution?

或者是否有任何要求在解决方案中使用any()?

#4


2  

You need any if you want at least one of each in the string:

如果你想要字符串中至少有一个,你需要任何一个:

if any(x.isalpha() for x in string) and any(x.isspace() for x in string):

If you want at least one of each but no other characters you can combine all,any and str.translate , the following will only return True if we have at least one space, one alpha and contain only those characters.

如果你想要至少一个但没有其他字符你可以组合all,any和str.translate,如果我们至少有一个空格,一个alpha并且只包含那些字符,则以下只会返回True。

 from string import ascii_letters

 s = input("Enter a string: ")

 tbl = {ord(x):"" for x in ascii_letters + " "}

if all((any(x.isalpha() for x in s),
   any(x.isspace() for x in s),
   not s.translate(tbl))):
    print("all good")

Check if there is at least one of each with any then translate the string, if the string is empty there are only alpha characters and spaces. This will work for upper and lowercase.

检查是否至少有一个带有any然后翻译字符串,如果字符串为空,则只有字母字符和空格。这适用于大写和小写。

You can condense the code to a single if/and:

您可以将代码压缩为单个if /和:

from string import ascii_letters

s = input("Enter a string: ")
s_t = s.translate({ord(x):"" for x in ascii_letters})

if len(s_t) < len(s) and s_t.isspace():
    print("all good")

If the length of the translated string is < original and all that is left are spaces we have met the requirement.

如果翻译的字符串的长度是 并且剩下的全部是空格,那么我们已满足要求。

Or reverse the logic and translate the spaces and check if we have only alpha left:

或者反转逻辑并翻译空格并检查我们是否只剩下alpha:

s_t = s.translate({ord(" "):"" })
if len(s_t) < len(s) and s_t.isalpha():
    print("all good")

Presuming the string will always have more alphas than spaces, the last solution should be by far the most efficient.

假设字符串将始终具有比空格更多的alpha,最后的解决方案应该是最有效的。

#5


0  

    string = input("Enter a string: ")
    st1=string.replace(" ","").isalpha()
    if (st1):
        print("Only alphabetical letters and spaces: yes")
    else:
        print("Only alphabetical letters and spaces: no")

#1


13  

A character cannot be both an alpha and a space. It can be an alpha or a space.

角色不能同时是角色和空间。它可以是alpha或空格。

To require that the string contains only alphas and spaces:

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

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")

To require that the string contains at least one alpha and at least one space:

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

if any(x.isalpha() for x in string) and any(x.isspace() for x in string):

To require that the string contains at least one alpha, at least one space, and only alphas and spaces:

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

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)):

Test:

>>> 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

#2


3  

The correct solution would use an or.

正确的解决方案是使用或。

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")

Although you have a string, you are iterating over the letters of that string, so you have one letter at a time. So, a char alone cannot be an alphabetical character AND a space at the time, but it will just need to be one of the two to satisfy your constraint.

虽然你有一个字符串,但你正在遍历该字符串的字母,所以你一次只能有一个字母。因此,单独的char不能是字母字符和当时的空格,但它只需要是满足约束条件的两个中的一个。

EDIT: I saw your comment in the other answer. alphabet = string.isalpha() return True, if and only if all characters in a string are alphabetical letters. This is not what you want, because you stated that you want your code to print yes when execute with the string please, which has a space. You need to check each letter on its own, not the whole string.

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

Just to convince you that the code is indeed correct (well, ok, you need to execute it yourself to be convinced, but anyway):

只是为了让你相信代码确实是正确的(好吧,你需要自己执行才能确信,但无论如何):

>>> 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

EDIT 2: Judging from your new comments, you need something like this:

编辑2:从你的新评论来看,你需要这样的东西:

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

or

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


2  

Actually, it is an pattern matching exercise, so why not use pattern matching?

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

import re
r = re.compile("^[a-zA-Z ]*$")
def test(s):
    return not r.match(s) is None  

Or is there any requirement to use any() in the solution?

或者是否有任何要求在解决方案中使用any()?

#4


2  

You need any if you want at least one of each in the string:

如果你想要字符串中至少有一个,你需要任何一个:

if any(x.isalpha() for x in string) and any(x.isspace() for x in string):

If you want at least one of each but no other characters you can combine all,any and str.translate , the following will only return True if we have at least one space, one alpha and contain only those characters.

如果你想要至少一个但没有其他字符你可以组合all,any和str.translate,如果我们至少有一个空格,一个alpha并且只包含那些字符,则以下只会返回True。

 from string import ascii_letters

 s = input("Enter a string: ")

 tbl = {ord(x):"" for x in ascii_letters + " "}

if all((any(x.isalpha() for x in s),
   any(x.isspace() for x in s),
   not s.translate(tbl))):
    print("all good")

Check if there is at least one of each with any then translate the string, if the string is empty there are only alpha characters and spaces. This will work for upper and lowercase.

检查是否至少有一个带有any然后翻译字符串,如果字符串为空,则只有字母字符和空格。这适用于大写和小写。

You can condense the code to a single if/and:

您可以将代码压缩为单个if /和:

from string import ascii_letters

s = input("Enter a string: ")
s_t = s.translate({ord(x):"" for x in ascii_letters})

if len(s_t) < len(s) and s_t.isspace():
    print("all good")

If the length of the translated string is < original and all that is left are spaces we have met the requirement.

如果翻译的字符串的长度是 并且剩下的全部是空格,那么我们已满足要求。

Or reverse the logic and translate the spaces and check if we have only alpha left:

或者反转逻辑并翻译空格并检查我们是否只剩下alpha:

s_t = s.translate({ord(" "):"" })
if len(s_t) < len(s) and s_t.isalpha():
    print("all good")

Presuming the string will always have more alphas than spaces, the last solution should be by far the most efficient.

假设字符串将始终具有比空格更多的alpha,最后的解决方案应该是最有效的。

#5


0  

    string = input("Enter a string: ")
    st1=string.replace(" ","").isalpha()
    if (st1):
        print("Only alphabetical letters and spaces: yes")
    else:
        print("Only alphabetical letters and spaces: no")