Search code examples
pythonassert

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.


Solution

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