Python keyword Module
Example
Test if a word is a Python keyword and print how many keywords exist:
import keyword
print(keyword.iskeyword("for"))
print(len(keyword.kwlist))
Try it Yourself »
Definition and Usage
The keyword module provides a list of Python keywords and helper functions to test whether a string is a keyword.
Use it to validate identifiers, generate code, or for tooling and linters that need to distinguish between keywords and names.
Members
Member | Description |
---|---|
iskeyword() | Return True if the string is a Python keyword. |
issoftkeyword() | Return True if the string is a soft keyword (e.g., "match", "case"). |
kwlist | List of Python keywords. |
softkwlist | List of Python soft keywords. |