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

前端 未结 7 1675
我在风中等你
我在风中等你 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条回答
  •  一向
    一向 (楼主)
    2020-11-29 19:56

    I liked the answer provided by @jean-françois-fabre, but it is incomplete.
    His approach will work, but only if the text contains purely lower- or uppercase letters:

    >>> text = "(555).555-5555 extA. 5555"
    >>> text.islower()
    False
    >>> text.isupper()
    False
    

    The better approach is to first upper- or lowercase your string and then check.

    >>> string1 = "(555).555-5555 extA. 5555"
    >>> string2 = '555 (234) - 123.32   21'
    
    >>> string1.upper().isupper()
    True
    >>> string2.upper().isupper()
    False
    

提交回复
热议问题