Content · Programming · Python

Python str.isidentifier Method: Usage and Examples

str.isidentifier() checks whether a nonempty string follows Python identifier syntax rules. A valid identifier may still be a reserved keyword, so use keyword.iskeyword() when validating proposed variable names. Learn.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

This guide explains str.isidentifier Method: Usage and Examples through a focused example and practical reasoning. No prior knowledge of this specific operation is required.

Understanding the Concept


str.isidentifier() checks whether a nonempty string follows Python identifier syntax rules. A valid identifier may still be a reserved keyword, so use keyword.iskeyword() when validating proposed variable names.

When You Would Use This


Check suggested names for variables in a code generator.

Step-by-Step Example


import keyword
suggested_names = ["total_price", "2prices", "class", "user-name"]
for proposed_name in suggested_names:
    is_usable_name = proposed_name.isidentifier() and not keyword.iskeyword(proposed_name)
    print(proposed_name, is_usable_name)

How the Example Works


total_price is valid. A leading digit and hyphen are invalid, while class is syntactically an identifier but cannot be used as a variable name because it is a keyword.

Example Output


total_price True
2prices False
class False
user-name False

Try It Yourself


Test "user_1", "for", and "_private" as proposed Python names.

Common Beginner Mistakes


  • Assuming isidentifier() rejects Python keywords.
  • Using isalnum() and accidentally rejecting underscores.
  • Assuming an identifier must consist of ASCII letters only.

Key Takeaways


  • str.isidentifier() checks whether a nonempty string follows Python identifier syntax rules.
  • total_price is valid.
  • Avoid this mistake: Assuming isidentifier() rejects Python keywords.
Share this resourceSend this page to someone who may find it useful.