pythonregex

In Python, how do I check if a string has alphabets or numbers?


If the string has an alphabet or a number, return true. Otherwise, return false.

I have to do this, right?

return re.match('[A-Z0-9]',thestring)

Solution

  • Use thestring.isalnum() method.

    >>> '123abc'.isalnum()
    True
    >>> '123'.isalnum()
    True
    >>> 'abc'.isalnum()
    True
    >>> '123#$%abc'.isalnum()
    >>> a = '123abc' 
    >>> (a.isalnum()) and (not a.isalpha()) and (not a.isnumeric())
    True
    >>>