简体   繁体   中英

Check if string has numbers, letters and spaces

I'm trying to come up with an assert statement that checks whether a nonempty string, s, contains alphanumeric characters and spaces:

    assert s.isalnum()

I understand that this will return False if there are spaces because it checks that whether every character is either a letter or number. How do I fix this?

EDIT: For clarification, I'm trying to create an assert statement that checks whether a nonempty string contains alphanumeric characters and/or spaces. For example, " a 4 bc " and "ab" should both return True.

You could use all , to check if every character is alphanumeric or space:

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)

Output

True
False

If it most contain spaces and alphanumeric characters, you can do:

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

Output

True
False
False

Also note, as mentioned by @Chris_Rands that this .isspace considers tabs as whitespaces.

您可以删除空格进行测试:

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

This would be one way

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)

Maybe this is what you want:

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

Strings for testing:

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

Check whether a string contains any space:

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

Check whether a string contains any digit, you can use any function and str.isdigit function:

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

Check whether a string contains any alphabetic character, you can use any function and str.isalpha function:

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

Check whether a string contains any number, or any alphabetic character or any space :

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

If you want to use assert statement, you can use any combination of them:

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

Of course, if you don't like these methods, you can simply use assert like this:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM