← Back to Python Course | Chapter 1: Basics | Lesson 14 of 14

Python Variable Names

Python variable assignment (x = 5) is simple, but not every string of characters is actually a legal variable name -- Python enforces specific rules about what a name can contain and what it cannot be, and understanding those rules avoids confusing SyntaxError messages when a name accidentally breaks one of them.

The Basic Naming Rules

A legal Python variable name must start with a letter (a-z, A-Z) or an underscore (_), and every character after that first one can be a letter, a digit, or an underscore -- no spaces, hyphens, or other punctuation are allowed anywhere in the name.

Note: When in doubt about whether a name is legal, remember the simple rule: must start with a letter or underscore, and only letters/digits/underscores after that.

Warning: A variable name containing a space (like my var) or a hyphen (like my-var) is not just bad style -- it is a genuine SyntaxError, since Python would try to parse it as multiple separate tokens.

Example: The Basic Naming Rules

python
_valid_name = 1
name2 = 2
print(_valid_name, name2)

Names Cannot Be Reserved Keywords

Python reserves certain words (like if, else, for, while, class, def, return, and True/False/None) exclusively for the language's own syntax -- attempting to use any of them as a variable name produces a SyntaxError, since Python cannot tell whether you mean the keyword or a variable.

Note: Use the keyword module (import keyword; print(keyword.kwlist)) to see the complete, current list of reserved words for your Python version.

Warning: Adding an underscore or number to a keyword (like class_ or class1) sidesteps the restriction and is technically legal, though it can still read confusingly to someone expecting the actual keyword.

Example: Names Cannot Be Reserved Keywords

python
import keyword
print(keyword.iskeyword("class"))
class_ = "workaround"
print(class_)

Case Sensitivity

Python treats uppercase and lowercase letters as completely distinct, meaning age, Age, and AGE are three separate, independent variables that can each hold a different value at the same time -- a detail that matters both for avoiding bugs and for following naming conventions consistently.

Note: Pick one consistent casing convention (like all-lowercase with underscores) for your own variables and stick to it throughout a project, to avoid accidental case-mismatch bugs.

Warning: A typo that only differs in case (reading Total instead of the actual variable total) produces a NameError rather than silently reading the wrong value, but it is still a frustrating bug to track down.

Example: Case Sensitivity

python
age = 25
Age = 30
AGE = 35
print(age, Age, AGE)

Python Naming Conventions (PEP 8)

Beyond the strict legal rules, Python's official style guide (PEP 8) recommends specific naming conventions: snake_case (lowercase with underscores) for variables and functions, UPPER_SNAKE_CASE for constants, and PascalCase for class names -- following these makes your code instantly recognizable and consistent with the wider Python community.

Note: Follow snake_case for variables and functions by default -- it is by far the most common convention in Python code and what most other developers will expect.

Warning: Mixing naming conventions inconsistently within the same project (some snake_case, some camelCase) is not a syntax error, but it makes code noticeably harder to read and maintain.

Example: Python Naming Conventions (PEP 8)

python
user_count = 5         # snake_case
MAX_RETRIES = 3          # UPPER_SNAKE_CASE

class UserAccount:        # PascalCase
    pass

print(user_count, MAX_RETRIES)

Multi-Word Names: Readability Matters

When a variable represents more than one concept (like a user's total order price), joining the words with underscores (total_order_price) instead of running them together (totalorderprice) or abbreviating unclear (torpr) keeps the name both legal and genuinely readable to whoever reads the code later, including your future self.

Note: Prefer a slightly longer, clearly descriptive name over a short, cryptic abbreviation -- the extra typing pays for itself the first time someone else (or you, months later) has to read the code.

Warning: An overly abbreviated name (like custId instead of customer_id) saves a few keystrokes now but costs real time later when someone has to guess what it means.

Example: Multi-Word Names: Readability Matters

python
total_order_price = 49.99  # clear
print(total_order_price)
Common Mistakes
  1. Starting a variable name with a digit (like 2cats), which Python rejects outright with a SyntaxError, since names must begin with a letter or underscore.
  2. Using a reserved keyword (like class, if, or return) as a variable name, which also produces a SyntaxError since keywords are reserved for the language itself.
  3. Forgetting that Python variable names are case-sensitive -- age, Age, and AGE are three completely distinct variables, not the same one.
Chapter Summary
  • A variable name must start with a letter or an underscore, and can only contain letters, digits, and underscores after that.
  • Variable names cannot be one of Python's reserved keywords (like if, for, class, or True).
  • Variable names are case-sensitive, so myVar and myvar refer to two entirely different variables.
Browser Support

Python variable naming rules are part of the core language syntax and apply identically in every Python version and environment.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.