检查字符串是否包含数字,字母和空格

时间:2018-11-28 10:13:52

标签: python assert

我正试图提出一个assert语句,以检查非空字符串s是否包含字母数字字符和空格:

    assert s.isalnum()

我知道,如果有空格,它将返回False,因为它检查每个字符是字母还是数字。我该如何解决?

编辑:为澄清起见,我试图创建一个assert语句来检查非空字符串是否包含字母数字字符和/或空格。例如,“ a 4 bc”和“ ab”都应返回True。

4 个答案:

答案 0 :(得分:2)

您可以使用all来检查每个字符是字母数字还是空格:

text = "apple and 123"

result = all(c.isalnum() or c.isspace() for c in text)
print(result)

text = "with ."

result = all(c.isalnum() or c.isspace() for c in text)
print(result)

输出

True
False

如果它最多包含空格字母数字字符,则可以执行以下操作:

def only_alnum_and_spaces(t):
    counts = {"spaces" : 0, "alnums": 0}
    for c in t:
        if c.isalnum():
            counts["alnums"] += 1
        elif c.isspace():
            counts["spaces"] += 1
        else:
            return False
    return counts["alnums"] > 0 and counts["spaces"] > 0

print(only_alnum_and_spaces("apple and 123"))
print(only_alnum_and_spaces("with ."))
print(only_alnum_and_spaces("appleand123"))

输出

True
False
False

还请注意,如@Chris_Rands所述,此.isspace将制表符视为空白。

答案 1 :(得分:0)

您可以删除要测试的空格:

assert s.replace(" ","").isalnum()

答案 2 :(得分:0)

这将是一种方式

def customIsAlnum(String[] words):
    if(len(words) > 1):
       for(word in words):
           if(!word.isalnum()):
               return false
    else:
        return false
    return true


String str = "chelsea scored 3 goals"
String[] words = str.split()
print customIsAlnum(words)

答案 3 :(得分:0)

也许这就是您想要的:

assert any(substr.issapce() or substr.isdigit() or substr.isalpha() for substr in s)

测试字符串:

>>> s1 = '123 45    abc  67  d'
>>> s2 = '123456'
>>> s3 = 'abcd'
>>> s4 = ':?--==++'

检查字符串是否包含空格:

>>> def hasAnySpace(str):
...   return ' ' in str
...
>>> hasAnySpace(s1)
True
>>> hasAnySpace(s2)
False
>>> hasAnySpace(s3)
False
>>> hasAnySpace(s4)
False

检查字符串是否包含数字,可以使用any函数和str.isdigit函数:

>>> def hasAnyDigit(str):
...   return any (substr.isdigit() for substr in str)
...
>>> hasAnyDigit(s1)
True
>>> hasAnyDigit(s2)
True
>>> hasAnyDigit(s3)
False
>>> hasAnyDigit(s4)
False

检查字符串是否包含字母字符,可以使用any函数和str.isalpha函数:

>>> def hasAnyAlpha(str):
...   return any(substr.isalpha() for substr in str)
...
>>> hasAnyAlpha(s1)
True
>>> hasAnyAlpha(s2)
False
>>> hasAnyAlpha(s3)
True
>>> hasAnyAlpha(s4)
False

检查字符串是否包含任何数字,字母字符或空格

>>> def hasAnyAlNumSpace(str):
...   return any(substr.isalpha() or substr.isdigit() or substr.isspace() for substr in str)
...
>>> hasAnyAlNumSpace(s1)
True
>>> hasAnyAlNumSpace(s2)
True
>>> hasAnyAlNumSpace(s3)
True
>>> hasAnyAlNumSpace(s4)
False

如果要使用assert语句,可以使用它们的任意组合:

>>> assert hasAnySpace(s1) or hasAnyDigit(s1) or hasAnyAlpha(s1) 
>>> assert hasAnySpace(s2) or hasAnyDigit(s2) or hasAnyAlpha(s2)
>>> assert hasAnySpace(s3) or hasAnyDigit(s3) or hasAnyAlpha(s3)
>>> assert hasAnySpace(s4) or hasAnyDigit(s4) or hasAnyAlpha(s4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>>
>>> assert hasAnySpace(s1)
>>> assert hasAnySpace(s2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>>
>>> assert hasAnyAlNumSpace(s1)
>>> assert hasAnyAlNumSpace(s2)
>>> assert hasAnyAlNumSpace(s3)
>>> assert hasAnyAlNumSpace(s4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError

当然,如果您不喜欢这些方法,则可以简单地使用assert:

assert ' ' in s1    
assert any(substr.isdigit() for substr in s1)
assert any(substr.isalpha() for substr in s1)
assert (' ' in s1) or any(substr.isdigit() or substr.isalpha() for substr in s1)
assert any(substr.issapce() or substr.isdigit() or substr.isalpha() for substr in s1)