Python Glossary Русский

all Python 3Input & outputfunction

keyword.iskeyword()

variables

Checks whether a string is a Python keyword (if, class, None…) that cannot be used as a variable name.

Syntax

keyword.iskeyword(s)

Soft keywords such as match, case, _ and type do not count: iskeyword('match') is False, and the name can indeed be used. True, False and None are keywords, while print and list are not and can be shadowed (though they should not be). The check is case-sensitive. A valid variable name is s.isidentifier() and not keyword.iskeyword(s).

Examples

import keyword
print(keyword.iskeyword('class'), keyword.iskeyword('None'))  # → True True
print(keyword.iskeyword('print'), keyword.iskeyword('match'))  # → False False
name = 'lambda'
print(name.isidentifier() and not keyword.iskeyword(name))  # → False
print(len(keyword.kwlist))  # → 35

See also

Open in the full glossarydocs.python.orgReport an inaccuracy