keyword

Il modulo offre supporto per le parole chiave del linguaggio

keyword.iskeyword(s) La stringa s è una parola chiave?
keyword.kwlist La lista delle parole chiave del linguaggio

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

Esempio 1

import keyword

print(keyword.iskeyword("class"))  # True
print(keyword.iskeyword("clas" ))  # False

Esempio 2

import keyword

for kw in keyword.kwlist:
    print(kw, end="\t")

Produce

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