Python Keywords: List and How to Check

Choosing a name in Python starts with checking whether the language reserves that spelling. The standard-library keyword module checks hard keywords and soft keywords, whose grammar role depends on context.

I’ll show you how to check both categories in the interpreter you run and distinguish keywords from built-in names such as next.

TL;DR

Python keywords are reserved spellings with grammar meaning. The interpreter’s keyword module gives the hard-keyword list and checks contextual soft keywords.

  • Use keyword.kwlist for the current hard-keyword list.
  • Use keyword.iskeyword() and keyword.issoftkeyword() to test a name.
  • Soft keywords such as match are reserved only in specific grammar contexts.

What Is a Python Keyword?

A Python keyword is a word with a defined role in the language grammar. For example, if begins a conditional and def introduces a function definition.

An identifier is a name the program uses for an object or definition, such as ready or total. The Python identifiers guide covers the rules for choosing names.

Soft keywords such as match take a grammar role in defined contexts. The lexical reference documents each context for the Python release you use.

Keyword names are case-sensitive. The spelling True is a keyword, while true is a different identifier. Changing capitalization does not create a lowercase form of the same keyword.

How to Check Python Keywords in Your Interpreter

The standard-library keyword module reports the current interpreter list. I ran a program that counts hard keywords and checks hard and soft status. This runtime reports 35 hard keywords, while match is soft.

Step 1: Import the keyword module

Import keyword in the script where you want to inspect names. It is part of Python’s standard library, so this check uses no third-party package.

import keyword

print(len(keyword.kwlist))
print(keyword.kwlist)

kwlist is the sequence of hard keywords defined for that interpreter.

Step 2: Test a spelling before using it as a name

Call keyword.iskeyword() with a string to check its hard-keyword status. The same test helps a code generator or linter validate a proposed name.

import keyword

print(keyword.iskeyword("for"))
print(keyword.iskeyword("next"))

Running python3 keyword_check.py produced these captured results:

Python keywords: 35
Reserved check for 'for': True
Reserved check for 'match': False
Soft-keyword check for 'match': True
Soft keywords: ['_', 'case', 'match', 'type']
Terminal output checking Python keywords

The first result marks for as reserved. The next shows next is available as an identifier, although it names a built-in function.

Step 3: Check soft keywords separately

Python 3.9 added support for testing soft keywords through the keyword module.

import keyword

print(keyword.iskeyword("match"))
print(keyword.issoftkeyword("match"))
print(keyword.softkwlist)

I ran the combined checks in keyword_check.py with python3 keyword_check.py. The captured output was:

Python keywords: 35
Reserved check for 'for': True
Reserved check for 'match': False
Soft-keyword check for 'match': True
Soft keywords: ['_', 'case', 'match', 'type']
Terminal output checking Python keywords

The keyword module documentation describes the API and its interpreter-specific result.

Python Keyword Rules and Common Confusions

The language reference distinguishes reserved words from soft keywords and documents where the latter are recognized.

TermWhat it meansExample
Hard keywordReserved word in Python grammarfor
Soft keywordKeyword-like spelling in a defined contextmatch, case
Built-in nameName provided by the builtins namespaceprint, next
Keyword argumentArgument passed using a parameter nameround(number=2.5)

A keyword argument is a named function-call argument. In round(number=2.5), number names a parameter in a function call. The syntax borrows the ordinary term “keyword” because the argument is matched by name.

Soft keywords are context-bound. For example, match and case have grammar roles in a match statement, yet keyword.iskeyword(“match”) returns False. The separate soft-keyword function identifies that category.

Soft-keyword membership varies by release. The current language reference documents match, case and _ in match contexts, type in a type statement, and lazy before an import statement. Merge is not on that list.

Built-in names such as print and next belong to the builtins namespace rather than the keyword grammar. Python permits an assignment such as next = 3, but that binding shadows the built-in name in its scope. A syntax highlighter may color keywords and built-ins differently, but the visual choice is editor-specific rather than a language rule.

Operators can be mistaken for words. The symbol // performs floor division. The keyword in tests membership and supports iteration, as in value in items.

When building a name validator, test hard keywords with iskeyword() and then apply Python’s identifier rules. Avoid rejecting every word highlighted by an editor, because an editor may color built-ins and soft keywords as well. The Python variables guide explains how names refer to values, and the Python statements guide gives more syntax context.

Python tokenizes source, then parses those tokens using its grammar. Soft-keyword status depends on context, so check both categories when validating generated names.

A variable named match can be legal in an assignment, while the spelling has a grammar role at the start of a match statement. Python identifiers start with a letter or underscore, with permitted Unicode letters also supported. Later characters may include digits, but the first character cannot be a digit.

Python leaves many useful names available. The built-in print comes from the builtins namespace and can be reassigned, though later calls in that scope then use the replacement. A grammar word such as if keeps its syntax role and cannot be assigned as an ordinary name.

The keyword is tests whether two references point to the same object, while == compares values for equality. Separately created lists can have equal contents and remain distinct objects. Use is for identity checks such as comparing a value with None, not as a general substitute for equality.

QuestionCheck or ruleWhat to expect
Is a spelling reserved?keyword.iskeyword(name)True for a hard keyword
Does it have contextual grammar meaning?keyword.issoftkeyword(name)True for a soft keyword listed by this interpreter
Can a word be called?Check the builtins namespace or imported definitionsBuilt-in status does not make a word a keyword
Does a function call name a parameter?Inspect the function signatureThat is a keyword argument, not a reserved word

A name-checking tool should separate syntax validation from project policy. A negative hard-keyword result does not test the soft-keyword category. The name may still fail identifier syntax, shadow a built-in, break a style rule or be undefined in the current scope.

Python exposes a string’s identifier validity through str.isidentifier(). A validator can combine that method with the keyword module, then apply any additional naming rules required by its project. These checks answer different questions and should not be collapsed into one test.

import keyword

for name in ("count", "for", "3items"):
    print(name, name.isidentifier(), keyword.iskeyword(name))

This snippet distinguishes a valid ordinary identifier from a reserved word and from a spelling that fails identifier syntax. A production name validator can add project-specific checks without changing Python’s own keyword classification.

A keyword argument names a function parameter in a call, as in round(number=2.5). The function signature determines which names the call accepts.

For a quick name check, match the interpreter that runs your program. I use that runtime because installed Python versions can report different keyword data.

When comparing a keyword list online, check the release described by the page and the environment where you need the answer. New grammar features can add contextual names without adding them to the hard-keyword list, so a list that omits soft keywords may still be correct for its stated purpose. Documentation for the current release is the source for grammar details, while the local keyword module is the direct check for the running interpreter.

Syntax highlighting can help when reading code, but its colors are not a test. Editors may highlight language keywords, built-in names, types or identifiers using similar styles. If code generation or static analysis needs a definitive answer, call the standard-library check rather than infer status from color.

A SyntaxError can reveal an invalid use of a hard keyword, but it is a poor first-line name checker. The parser location may follow the underlying mistake, and a failed snippet may contain another syntax problem. The keyword module gives a focused result before code is generated or executed.

Common Python Keyword Edge Cases

Interpreter choice and name classification account for common surprises. Check each case with a short expression before relying on a generated name.

  • Different Python launcher: keyword counts can differ. Run the check with your program’s launcher.
  • Soft keyword used as a name: grammar context changes its role. Check keyword.issoftkeyword() and the release reference.
  • Invalid identifier: a name begins with a digit. Check name.isidentifier().
import keyword

name = "3items"
print(name.isidentifier(), keyword.iskeyword(name))

Conclusion: Check the Interpreter You Run

Check the interpreter for its current hard-keyword list, and test the soft-keyword category separately when needed.

FAQ

These answers clarify the count, common name checks and capitalization.

How many keywords are there in Python?

The count depends on your Python version. Run len(keyword.kwlist) to count the hard keywords in the interpreter you are using.

Is next a keyword in Python?

No. next is a built-in function name, not a hard keyword. Check with keyword.iskeyword(“next”).

Are Python keywords case-sensitive?

Yes. Keyword spellings are case-sensitive. True is a keyword, while true is a different name.

Is match a keyword in Python?

match is a soft keyword in the pattern-matching context. It is not a hard keyword, so keyword.iskeyword(“match”) returns False in the checked interpreter.

Pankaj Kumar
Pankaj Kumar

I have been working on Python programming for more than 12 years. At AskPython, I share my learning on Python with other fellow developers.

Articles: 259