How to Get the list of all Python keywords?

Python – Get the list of all keywords programmatically

To get the list of all keywords of Python programmatically, you can use kwlist of keyword library.

Following is the quick code snippet to get the list of all keywords.

import keyword
s = keyword.kwlist

kwlist returns sequence containing all the keywords defined for the interpreter.

Examples

1. Get all Python Keywords programmatically

In the following example, we will get all the Python keywords using kwlist property and print them using a for loop.

Python Program

import keyword

# Get all keywords
keywords = keyword.kwlist

# Pprint the keywords
for key in keywords:
    print(key)
Run Code Copy

Output

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

This is the list of all keywords in Python.

Summary

In this tutorial of Python Examples, we learned how to get the list of all Python keywords programmatically.

Code copied to clipboard successfully 👍