Python Variable Names
In this page:
valid_name = value # letters, digits, underscore; cannot start with a digit
_private_name = value
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.
Example: The Basic Naming Rules
_valid_name = 1 # starts with an underscore, which is allowed
name2 = 2 # letters followed by a digit, also allowed
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.
Example: Names Cannot Be Reserved Keywords
import keyword
print(keyword.iskeyword("class")) # True, "class" is reserved
class_ = "workaround" # trailing underscore avoids clashing with the keyword
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.
Example: Case Sensitivity
age = 25
Age = 30
AGE = 35 # age, Age, and AGE are three separate variables
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.
Example: Python Naming Conventions (PEP 8)
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.
Example: Multi-Word Names: Readability Matters
total_order_price = 49.99 # clear
print(total_order_price)
- 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.
- 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.
- Forgetting that Python variable names are case-sensitive -- age, Age, and AGE are three completely distinct variables, not the same one.
- 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.
Python variable naming rules are part of the core language syntax and apply identically in every Python version and environment.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: