Guide Python Beginner

4.7 Identifiers

The naming rules for Python identifiers, PEP 8 naming conventions, valid vs invalid identifier examples, and why reserved words can't be used as names.

2 min read

What Are Identifiers?

What Is It?

Identifiers are the names you give to variables, functions, classes, and modules.

What Are the Rules?

  • It must start with a letter or underscore (not a digit)
  • It may contain letters, digits, and underscores after that
  • Identifiers are case-sensitive
  • It cannot be a keyword

Naming Conventions

See 4.16 Coding Standards (PEP 8) for the full naming table — snake_case for variables/functions, PascalCase for classes, UPPER_SNAKE_CASE for constants.

Valid vs Invalid Identifiers

How Is It Used?

>>> "valid_name".isidentifier()
True
>>> "2invalid".isidentifier()      # cannot start with a digit
False
>>> "my-var".isidentifier()        # hyphens are not allowed
False

Reserved Words

The 35 keywords from 4.6 Keywords cannot be used as identifiers, even though they otherwise look like valid names — Python’s parser treats them as fixed syntax rather than user-defined names.

Quick Interview Answer

“An identifier must start with a letter or underscore, contain only letters/digits/underscores after that, and can’t be a keyword. Identifiers are case-sensitive — total and Total are different names. Use str.isidentifier() to check programmatically, and keyword.iskeyword() to check whether a valid-looking name is actually reserved.”

Common Mistakes

  • Starting an identifier with a digit (2invalid) — always a SyntaxError.
  • Using a hyphen instead of an underscore (my-var) — Python parses the hyphen as a minus operator, not part of the name.
  • Assuming identifiers are case-insensitive, then being confused why Username and username are treated as two separate variables.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form