Search code examples
pythonstringisnumeric

Check if one character in string is numeric or is a letter [Python]


I want to check the string and see if at least one of the characters in the string is a letter or number. How would i do this?


Solution

  • You could use re.search

    if re.search(r'[a-zA-Z\d]', string):
    

    It will return a match object if the string contain at-least a letter or a digit.

    Example:

    >>> s = "foo0?"
    >>> re.search(r'[a-zA-Z\d]', s)
    <_sre.SRE_Match object; span=(0, 1), match='f'>
    >>> s = "???"
    >>> re.search(r'[a-zA-Z\d]', s)
    >>> s = "???0"
    >>> re.search(r'[a-zA-Z\d]', s)
    <_sre.SRE_Match object; span=(3, 4), match='0'>
    >>>