Python keyword: test whether a string is a Python keyword
Answer: The keyword module tests whether a string is a keyword in Python. iskeyword(s) returns True if s is a Python keyword. kwlist is the sequence of all keywords defined for the interpreter. issoftkeyword and softkwlist cover soft keywords.
Examples
import keyword
keyword.iskeyword('for')
Notes
Reference list
iskeywordreturns True if the string is a Python keyword.kwlistholds every keyword defined for the interpreter.issoftkeywordtests for a soft keyword.
See also
Claims
- id: C1 claim: The module tests whether a string is a Python keyword. source: S1 evidence: "Return True if s is a Python keyword"
- id: C2 claim: iskeyword returns True if s is a Python keyword. source: S2 evidence: "Return `
Trueif s is a Python :ref:keyword <keywords>`."
- id: C3 claim: issoftkeyword returns True if s is a soft keyword. source: S2 evidence: "Return `
Trueif s is a Python :ref:soft keyword <soft-keywords>`."
Sources
- [S1] keyword — Testing for Python keywords. Python docs. https://docs.python.org/3/library/keyword.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] keyword.rst. CPython docs. https://raw.githubusercontent.com/python/cpython/main/Doc/library/keyword.rst . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, keyword — Testing for Python keywords; CPython docs, keyword.rst. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, keyword — Testing for Python keywords; CPython docs, keyword.rst. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python keyword: test whether a string is a Python keyword." iinventedlight Library, iil:note:wgm9dxqp, version of 2026-11-18. https://iinventedlight.com/library/notes/python-keyword/. License: CC-BY-4.0.
@misc{iil_note_wgm9dxqp,
author = {{iinventedlight}},
title = {{Python keyword: test whether a string is a Python keyword}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-keyword/},
note = {Permanent ID iil:note:wgm9dxqp. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-keyword.table.json (table; CC-BY-4.0)
- python-keyword.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-keyword.md.