簡體   English   中英

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

[英]Checking if string is only letters and spaces - Python

試圖讓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")

我一直想please它的出現Only alphabetical letters and spaces: no我使用or代替and ,但只滿足一個條件。 我需要滿足這兩個條件。 那就是句子必須包含字母空格,但必須至少有一個。 不得包含任何數字。

我在這里缺少什么讓python返回字母和空格只包含在字符串中?

一個字符不能既是字母是空格。 它可以是 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")

要求字符串至少包含一個字母和至少一個空格:

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

要求字符串至少包含一個字母,至少一個空格,並且只包含字母和空格:

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

測試:

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

正確的解決方案是使用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")

盡管您有一個字符串,但您正在迭代該字符串的字母,因此一次只有一個字母。 所以,一個字符在當時不能是一個字母字符和一個空格,但它只需要成為兩者之一來滿足你的約束。

編輯:我在另一個答案中看到了您的評論。 alphabet = string.isalpha()返回True ,當且僅當字符串中的所有字符都是字母。 這不是你想要的,因為你說你希望你的代碼在使用字符串please執行時打印yes ,它有一個空格。 您需要單獨檢查每個字母,而不是整個字符串。

只是為了讓你相信代碼確實是正確的(好吧,好吧,你需要自己執行才能被說服,但無論如何):

>>> 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:從你的新評論來看,你需要這樣的東西:

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

或者

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

如果您想要字符串中的至少一個,則需要任何一個:

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

如果您想要每個字符中的至少一個但沒有其他字符,您可以組合allanystr.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")

檢查是否有每個都有至少一個any再轉換成字符串,如果字符串是空的,只有字母字符和空格。 這適用於大寫和小寫。

您可以將代碼壓縮為單個if/and

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

如果翻譯后的字符串長度< original 並且剩下的都是空格,我們就滿足了要求。

或者顛倒邏輯並轉換空格並檢查我們是否只剩下alpha:

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

假設字符串總是比空格有更多的字母,最后一個解決方案應該是迄今為止最有效的。

其實就是模式匹配練習,為什么不使用模式匹配呢?

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

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

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

您可以使用正則表達式來執行該操作

import re


name = input('What is your name ?')

if not re.match("^[a-z A-Z]*$", name):
    print("Only Alphabet and Space are allowed")
else:
    print("Hello" ,name)
// remove spaces and question is only alphanumeric
def isAlNumAndSpaces(string):
    return string.replace(" ","").isalnum()
strTest = input("Test a string: ")
if isAlNumAndSpaces(strTest):
    print("Only alphabetical letters and spaces: yes")
else:
    print("Only alphabetical letters and spaces: no")

使用這樣的模式匹配

import re alpha_regex = "^[az AZ]+$" if re.match(alpha_regex, string): print("Only alphabetical letters and spaces: yes") else: print("Only alphabetical letters and spaces: no")

isalpha()的使用沒有使用正則表達式那么具體。

isalpha 不僅會為 [a-zA-Z] 返回 true,還會為其他字母 unicode 返回 true。

使用bool(re.match('^[a-zA-Z\\s]+$', name)

試圖讓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")

我一直在嘗試please ,結果顯示為Only alphabetical letters and spaces: no我使用過or代替and ,但這僅滿足一個條件。 我需要兩個條件都滿足。 也就是說,該句子必須包含字母空格,但每種類型至少應包含一個。 不能包含任何數字。

我在這里缺少什么讓python同時返回字母和空格的字符串?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM