Python Reference Library →

Python String isidentifier() – Check for Valid Identifiers

Let’s look at string isidentifier() – check for valid identifiers

  1. Notice

    isidentifier() checks whether text has the lexical form of a Python identifier. A reserved keyword can still pass this check.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'user_name'
result = text.isidentifier()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'user_name'Object 2 · boolTrue
This step

isidentifier() checks whether text has the lexical form of a Python identifier. A reserved keyword can still pass this check.

Picture string isidentifier() – check for valid identifiers

isidentifier() checks whether text has the lexical form of a Python identifier. A reserved keyword can still pass this check.

Image brief

Alt text
A diagram for Python String isidentifier() – Check for Valid Identifiers. Instructions: text = 'user_name' result = text.isidentifier() print(result). Displayed result: True. The final example bindings are text --> object 1 (str); result --> object 2 (bool). object 1: 'user_name'; object 2: True
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String isidentifier() – Check for Valid Identifiers". Teaching focus: isidentifier() checks whether text has the lexical form of a Python identifier. A reserved keyword can still pass this check. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'user_name' result = text.isidentifier() print(result) | v DISPLAYED RESULT True FINAL BINDINGS text --> object 1 (str) result --> object 2 (bool) object 1: 'user_name' object 2: True Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String isidentifier() – Check for Valid Identifiers. Instructions: text = 'user_name' result = text.isidentifier() print(result). Displayed result: True. The final example bindings are text --> object 1 (str); result --> object 2 (bool). object 1: 'user_name'; object 2: True

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'user_name'
result = text.isidentifier()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'user_name'Object 2 · boolTrue
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
True

What do you expect this program to print?

What does this example display?

text = 'user_name'
result = text.isidentifier()
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

True

Follow the printed values in order. The output is: True

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

isidentifier() checks whether text has the lexical form of a Python identifier. A reserved keyword can still pass this check.

isidentifier() checks whether text has the lexical form of a Python identifier. A reserved keyword can still pass this check.

More examples and reference

Python String isidentifier() Method

The isidentifier() method in Python is used to check whether a given string is a valid Python identifier. An identifier is a name used to identify variables, functions, classes, modules, etc.

Syntax

string.isidentifier()

Parameters:

  • No parameters required.

Returns:

  • True if the string is a valid identifier.
  • False otherwise.

What is a Valid Identifier?

  • Only contains letters (A–Z, a–z), digits (0–9), and underscores (_)
  • Cannot start with a digit
  • Cannot contain spaces or special characters
  • Cannot be a Python keyword (though isidentifier() won't check for keywords)

Example 1: Valid Identifier

name = "my_variable"
print(name.isidentifier())
True

Example 2: Starts with a Digit

name = "1variable"
print(name.isidentifier())
False

Example 3: Contains a Space

name = "my var"
print(name.isidentifier())
False

Example 4: Underscore is Allowed

name = "_temp123"
print(name.isidentifier())
True

Use Cases

  • Validating variable names in dynamic code generation
  • Building programming tools or editors
  • Checking user input before creating variable-like data

Common Mistakes

  • Thinking it checks for Python keywords – it doesn't. "class" is a valid identifier technically, but cannot be used as a variable.
  • Confusing it with isalnum() – which only checks for letters and digits, not identifier rules.

Interview Tip

Be ready to explain the difference between isidentifier() and isalnum(), and what makes a valid identifier in Python.

Summary

  • isidentifier() checks if a string is a valid Python identifier
  • Returns True if valid, False if not
  • Useful for code generation, validation, and linters

Practice Problem

Write a program that takes input from the user and tells whether it's a valid identifier.

user_input = input("Enter a variable name: ")
if user_input.isidentifier():
    print("Valid identifier")
else:
    print("Invalid identifier")

What to remember

Connect what you noticed in the example with the idea below.

Remember

isidentifier() checks whether text has the lexical form of a Python identifier. A reserved keyword can still pass this check.

Watch out for this idea

isidentifier() alone guarantees that a name is not a reserved keyword.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →