Sitelet https://www.askpython.com/python/python-keywords-guide

Python Reserved Words: List and How to Check

Choosing a name for Python code starts with checking whether Python gives that name a special role. The keyword module lists strict keywords and checks whether a name is a keyword or a soft keyword.

I’ll explain the difference between those keyword types, then show how to check names in the active interpreter.

TL;DR: Check Python Reserved Words in Your Version

Use Python’s keyword module for the strict reserved-word list, and check soft keywords separately when syntax depends on context.

  • keyword.kwlist lists strict keywords for this interpreter.
  • keyword.iskeyword(name) checks strict keywords, while keyword.issoftkeyword(name) checks soft keywords.
  • Names such as print and list are built-ins, not strict keywords.

What Are Python Reserved Words?

Python reserved words are names with a defined role in the language grammar, so they cannot be assigned as ordinary identifiers. The Python language reference lists strict keywords such as class, def, if, and return.

Python 3.14.7 returned 35 entries in keyword.kwlist.

Some names are soft keywords: they act as keywords only in particular grammar contexts. The current language reference documents match, case, and the wildcard underscore in match statements, type in type statements, and lazy before import statements.

Built-ins such as print and list are separate from strict keywords. True, False, and None are constants in Python syntax.

For naming rules beyond reserved words, see AskPython’s Python identifiers guide and its isidentifier() guide.

How to Check Python Keywords Step by Step

The keyword module tests reserved status, while str.isidentifier() checks identifier form. Use both when a proposed name is uncertain.

Identifier spelling and scope also matter. A name can have valid syntax but still conflict with another name in the same scope.

Reference pages often place a contents list, a short abstract, related papers and additional resources beside the keyword list. A reference page should help you find the relevant section, but the Python language reference is the place to inspect grammar productions and their context.

Related reading can help with adjacent questions, but it should not replace the parser or standard-library check. AskPython’s identifier articles explain which strings have identifier form.

A long glossary can help when you want to study each word, but the direct test is quicker when checking one proposed name.

A reserved-word check answers one narrow question: whether the spelling has a special role in Python grammar. It does not choose a clear variable name for you. Names such as total, customer_name, and item_count tell a reader what a value represents, while a name like x may need extra context.

Keep the spelling compatible with Python and make its meaning visible. Start an identifier with a letter or underscore, then use letters, digits, or underscores. Python treats upper- and lowercase letters as different, so Customer and customer are separate names.

A familiar word is not necessarily reserved. Later calls to list() or print() may use a variable with that name instead of the built-in. Prefer records or message_text when those names describe the data.

Python tutorials and keyword references often include navigation, account settings, or newsletter prompts alongside the language material. Those controls do not affect the grammar or the keyword list. For the programming question, use the interpreter check and consult the language reference when a word behaves differently in a particular construct.

Step 1: List strict keywords and test names

Save this example as keyword_check_demo.py, then run python3 keyword_check_demo.py from the directory containing the file.

import keyword

print(f"Python keywords: {len(keyword.kwlist)}")
print("class is keyword:", keyword.iskeyword("class"))
print("print is keyword:", keyword.iskeyword("print"))
print("match is soft keyword:", keyword.issoftkeyword("match"))
print("custom_name is identifier:", "custom_name".isidentifier())

The output from that command is:

Python keywords: 35
class is keyword: True
print is keyword: False
match is soft keyword: True
custom_name is identifier: True
Terminal output showing strict and soft keyword checks
Python reports strict keywords and soft keywords through separate checks.

The run used Python 3.14.7. In that interpreter, match returns False from iskeyword() but True from issoftkeyword().

Step 2: Inspect the complete current list

Display keyword.kwlist to print the complete strict list.

import keyword

for name in keyword.kwlist:
    print(name)

For example, 2name and my-name fail identifier syntax even though neither spelling is a strict keyword.

Choosing a name is a separate step from checking whether Python accepts it. A helpful variable name conveys the value’s purpose without copying a keyword or obscuring a built-in. For a count, use item_count rather than count1 when the surrounding code does not make the meaning clear.

Do not rename an external field just to make its label look like a Python identifier. JSON, database rows, and other data can use labels that contain spaces or reserved words. Keep such labels as strings, then map them to valid local names where your code needs variables.

Tutorial sites may invite readers to sign in to change settings or receive a weekly Python tip. Those choices affect the site account, not Python syntax. To verify a word, check the interpreter you plan to use rather than relying on a copied list from another release.

Common Errors and What to Do Next

Python raises SyntaxError while parsing an assignment to class. The short message, invalid syntax, tells you the source does not fit the grammar, but it may not identify the reason in words.

Rename the local variable, or map an external field to a safe Python name. For data formats with a field named class, use a dictionary key such as record[“class”] or a library’s field-alias feature rather than writing class as a variable.

The following table shows which check distinguishes these cases:

NameCheckMeaning
classiskeyword() is TrueStrict keyword, cannot be an ordinary identifier
matchiskeyword() False, issoftkeyword() TrueContext-sensitive in supported Python syntax
printiskeyword() is FalseBuilt-in name, assignable but avoid shadowing
my-valueisidentifier() is FalseInvalid identifier form regardless of keywords

The keyword module records when soft-keyword checks became available in Python 3.9.

The caret marks where parsing stopped, which can be after the original mistake. Check the shown line and the line before it for an unfinished bracket, quote, or expression, then inspect names used as assignment targets.

For a learner at home, start with a practical question: which name should this variable have? Ask for a guess about print, then test the guess with keyword.iskeyword().

Next, try class as an assignment target and read the syntax error together. Ask what role class plays in a class definition, then compare that with a variable name such as class_name. The goal is to connect the word to the grammar, not memorize a long list before writing any code.

When a child finds a soft keyword such as match, use a small example from the Python reference that shows its special grammar position. Then compare it with a normal variable use. If the local interpreter has different syntax support, treat that as a version difference and check its documentation rather than calling the child’s answer wrong.

CaseWhat you may seeNext check
Strict keyword as a nameSyntaxError while parsingReplace it with another identifier
Built-in used as a variableLater calls may refer to your valueRename the variable to avoid shadowing
Invalid identifier charactersSyntaxError or False from isidentifier()Use letters, digits after the first character, and underscores
Soft keyword in an unexpected contextDifferent behavior by grammar and versionCheck the documented syntax for that Python version

For names built from user input, validate the spelling before using it as a Python identifier. A string with spaces or punctuation can still be a useful dictionary key. Keep external labels as strings, then map them to simple local names when your code needs identifiers.

A clear alternative such as class_name tells readers what the value represents.

When an error persists after renaming a keyword, check the whole statement rather than changing names at random. A missing colon, comma, or incomplete expression can produce the same broad syntax message. Make one correction, run the file again, and use the new caret location to choose what to inspect.

Conclusion: Check Names in the Active Interpreter

Start with the name you plan to assign, then test whether it is a strict keyword or a valid identifier.

The keyword module documentation and language reference document these rules. For further reading, see the Python identifier rules guide and isidentifier() guide.

FAQ

These answers address common questions about Python reserved words.

How many reserved words are in Python?

The count depends on the interpreter’s strict keyword list and version. Run len(keyword.kwlist) to check your current Python.

Is print a reserved word in Python?

No. print is a built-in function name, not a strict keyword. It can be assigned, but doing so can shadow the built-in.

Can I use class as a variable name in Python?

No. class is a strict keyword and using it as an assignment name causes a SyntaxError. Choose a different identifier.

Is match a Python keyword?

match can be a soft keyword in supported versions and grammar contexts. Check keyword.issoftkeyword() in the interpreter you use.

Ninad
Ninad

A Python and PHP developer turned writer out of passion. Over the last 6+ years, he has written for brands including DigitalOcean, DreamHost, Hostinger, and many others. When not working, you'll find him tinkering with open-source projects, vibe coding, or on a mountain trail, completely disconnected from tech.

Articles: 136