Check if a string is a valid keyword in Python


Python tip:

You can check whether some string is a reserved Python keyword with the keyword module.

An example👇

import keyword

""""
False       await       else        import      pass
None        break       except      in          raise
True        class       finally     is          return
and         continue    for         lambda      try
as          def         from        nonlocal    while
assert      del         global      not         with
async       elif        if          or          yield
"""

print(keyword.iskeyword("raise"))
# => True

print(keyword.iskeyword("foo"))
# => False