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

前端 未结 7 1682
我在风中等你
我在风中等你 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

    You can also do this in addition

    import re
    string='24234ww'
    val = re.search('[a-zA-Z]+',string) 
    val[0].isalpha() # returns True if the variable is an alphabet
    print(val[0]) # this will print the first instance of the matching value
    

    Also note that if variable val returns None. That means the search did not find a match

提交回复
热议问题