Skip to content

Module 2 · Python Syntax and Fundamentals — topic 8 of 14

Identifiers

Identifiers are the names you give to variables, functions and classes. Learn the exact rules for valid identifiers and how to test a name with isidentifier().

Beginner 10 min read·Lesson 22 of 306 in Python Programming

Learning objectives

  • State the rules for valid identifiers
  • Spot invalid names
  • Check names with str.isidentifier()

Definition

Identifiers are the names you give to variables, functions and classes. Learn the exact rules for valid identifiers and how to test a name with isidentifier().

Introduction

Whenever you create something you will refer to later, you give it a name. In Python these names are called identifiers. total_marks, student_name and calculate_grade are all identifiers.

Why it matters

An invalid identifier stops the whole program with a SyntaxError. Knowing the rules means you never waste time on that, and choosing names well later depends on knowing what is allowed.

Detailed explanation

An identifier may contain letters, digits and underscores.

It must not start with a digit: total2 is valid, 2total is not.

It must not be a keyword such as for or class.

Spaces, hyphens and symbols such as @, $ or % are not allowed. Use underscores instead: first_name, not first-name.

Identifiers are case-sensitive: Marks and marks are different.

Python 3 allows letters from other alphabets, such as naam or 名前, but English ASCII names are the convention in shared code.

Names beginning with underscores have conventional meanings you will meet later: _internal for private use and __init__ for special methods.

Some names are legal but unwise because they hide something Python already provides. Calling a variable list, str, sum or print replaces the built-in of that name for the rest of the file, so a later call such as list("abc") suddenly fails. Python won’t stop you, which is why it is worth knowing the common built-in names and choosing different ones, such as items, text or total.

Length is not limited, but very long names become hard to read. Aim for names that are as short as possible while still being completely clear: student_count is better than both sc and the_total_number_of_students_in_this_class.

Syntax

Python
valid_name = 1
_private = 2
name2 = 3
  • Letters, digits and underscores only.
  • The first character is a letter or underscore.
  • Not a keyword, no spaces or hyphens.

Code examples

Testing names

Python
print("total_marks".isidentifier())
print("2nd_place".isidentifier())
print("first-name".isidentifier())
Output
True
False
False

Same letters, different case

Python
marks = 72
Marks = 90
print(marks, Marks)
Output
72 90

More examples

Validating names from a form

Python
import keyword
def valid_name(name):
return name.isidentifier() and not keyword.iskeyword(name)
for candidate in ["roll_no", "roll no", "_temp", "9lives", "while", "café"]:
print(f"{candidate:<8} -> {valid_name(candidate)}")
Output
roll_no  -> True
roll no  -> False
_temp    -> True
9lives   -> False
while    -> False
café     -> True

Step-by-step explanation

  • isidentifier() checks the character rules but does not know about keywords.
  • The extra keyword check rejects reserved words such as while.
  • {candidate:<8} pads each name to 8 characters so the arrows line up.
  • café is valid because Python 3 allows non-ASCII letters.

Expected output

Running “Validating names from a form” prints exactly:

TextOutput
roll_no -> True
roll no -> False
_temp -> True
9lives -> False
while -> False
café -> True

Practical applications

  • Data tools convert spreadsheet headers such as “Total Marks” into identifiers like total_marks.
  • Form builders validate field names before generating code from them.
  • Linters warn about single-letter or misleading identifiers.

Best practices

  • Use underscores to separate words.
  • Stick to ASCII letters in shared code.
  • Make names descriptive enough that no comment is needed.

Common mistakes

Hyphen instead of underscore

Avoid

first-name = "Riya"

Prefer

first_name = "Riya"

Python reads first-name as first minus name, so the assignment fails.

Starting with a digit

Avoid

1st_attempt = 5

Prefer

attempt_1 = 5

Identifiers can contain digits but cannot begin with one.

Key takeaways

  • Identifiers use letters, digits and underscores and can’t start with a digit.
  • Keywords, spaces and hyphens are not allowed.
  • str.isidentifier() checks the character rules.

Try it yourself

  1. 1
    Which of these are valid: price$, _count, total_2, for, My Var? Check with isidentifier().
  2. 2
    Convert the heading "Date of Birth" into a valid identifier in code.
    Show hint

    lower() and replace(" ", "_")

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhich is a valid variable name?
Practise this topic