How can I check if a string contains ANY letters from the alphabet?

前端 未结 7 1686
我在风中等你
我在风中等你 2020-11-29 19:09

What is best pure Python implementation to check if a string contains ANY letters from the alphabet?

string_1 = \"(555).555-5555\"
string_2 = \"(555) 555 - 5         


        
7条回答
  •  猫巷女王i
    2020-11-29 19:57

    You can use islower() on your string to see if it contains some lowercase letters (amongst other characters). or it with isupper() to also check if contains some uppercase letters:

    below: letters in the string: test yields true

    >>> z = "(555) 555 - 5555 ext. 5555"
    >>> z.isupper() or z.islower()
    True
    

    below: no letters in the string: test yields false.

    >>> z= "(555).555-5555"
    >>> z.isupper() or z.islower()
    False
    >>> 
    

    Not to be mixed up with isalpha() which returns True only if all characters are letters, which isn't what you want.

    Note that Barm's answer completes mine nicely, since mine doesn't handle the mixed case well.

提交回复
热议问题