How can I use the startswith function to match any alpha character [a-zA-Z]. For example I would like to do this:
if line.startswith(ALPHA):
Do Something
If you want to match non-ASCII letters as well, you can use str.isalpha
:
if line and line[0].isalpha():
You can pass a tuple to startswiths()
(in Python 2.5+) to match any of its elements:
import string
ALPHA = string.ascii_letters
if line.startswith(tuple(ALPHA)):
pass
Of course, for this simple case, a regex test or the in
operator would be more readable.
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