Search code examples
python-3.x

how to see if a word starts with char g-z(python 3)


How do you see if a word starts with letters g-z using the .startswith() without doing a whole bunch of or's: for example is it possible to do this:

if word.startswith(g-z):

without doing this:

if word.startswith(h) or word.startswith(i) or word.startswith(j) etc...:

By the way I'm not allowed to import things


Solution

  • if re.match('[g-z]', word):
       ...
    

    Or if you really, really don't want to use re:

    if 'g' <= word[0] <= 'z':
       ...