There is no need to write a regex here. This is already builtin in Python with str.isalpha
:
Return True
if all characters in the string are alphabetic and there is at least one character, False
otherwise.
So we can check it with:
if your_string.isalpha():
pass
Note however that:
Note: str.isalpha
also includes diacritics, etc. For example:
>>> 'ä'.isalpha()
True
this is not per se a problem. But it can be something to take into account.
In case you do not want diacricics, you can for instance check that all characters have an ord(..)
less than 128 as well:
if your_string.isalpha() and all(ord(c) < 128 for c in your_string):
pass
The advantage of using builtins is that these are more self-explaining (isalpha()
clearly suggests what it is doing), and furthermore it is very unlikely to contain any bugs (I am not saying that other approaches do contain bugs, but writing something yourself, typically means it is not tested very effectively, hence it can still not fully cover edge and corner cases).