I'm trying to check if a string only contains letters, not digits or symbols.
For example:
>>> only_letters("hello") True >>> only_letters("he7lo") False
Use the test() method to check if a string contains only letters, e.g. /^[a-zA-Z]+$/. test(str) . The test method will return true if the string contains only letters and false otherwise.
Use string. ascii_letters if you use '[a-zA-Z]' regexps.
Simple:
if string.isalpha(): print("It's all letters")
str.isalpha()
is only true if all characters in the string are letters:
Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.
Demo:
>>> 'hello'.isalpha() True >>> '42hello'.isalpha() False >>> 'hel lo'.isalpha() False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With