Last modified: May 25, 2026
Python Variable Naming Rules
Variables are the building blocks of any Python program. They store data that your code can use and change.
But Python has strict rules for naming variables. If you break these rules, your code will not run.
This guide covers every rule you need to know. We will use short sentences and clear examples.
Let's start with the most important rules first.
Rule 1: Start with a Letter or Underscore
A variable name must start with a letter (a-z, A-Z) or an underscore (_).
It cannot start with a number. This is a hard rule in Python.
# Valid variable names
my_var = 10
_var = 20
myVariable = 30
# Invalid variable names
# 1var = 10 # SyntaxError
# 2nd_place = "silver" # SyntaxError
Starting with an underscore has special meaning. It often means the variable is private or internal.
Always start your variable names with a letter. This is the safest choice.
Rule 2: Only Use Letters, Numbers, and Underscores
After the first character, you can use letters, numbers (0-9), and underscores.
No spaces, hyphens, or special characters like @, !, #, or $ are allowed.
# Valid
user_name = "Alice"
user1 = "Bob"
my_variable_123 = 100
# Invalid
# user-name = "Charlie" # SyntaxError
# user@name = "Dave" # SyntaxError
# my variable = 200 # SyntaxError
This rule keeps variable names clean and easy to read. Python's parser expects this format.
Rule 3: Variable Names Are Case-Sensitive
Python treats uppercase and lowercase letters as different characters.
So name, Name, and NAME are three separate variables.
name = "Alice"
Name = "Bob"
NAME = "Charlie"
print(name) # Output: Alice
print(Name) # Output: Bob
print(NAME) # Output: Charlie
Alice
Bob
Charlie
Be careful with case. A typo in capitalization can cause a NameError.
For more on how variables work, see our guide on Python Variables Explained Simply.
Rule 4: Avoid Python Keywords
Python has reserved words called keywords. You cannot use them as variable names.
Keywords include if, else, for, while, class, def, return, True, False, None, and many more.
# Invalid - these are keywords
# if = 10 # SyntaxError
# for = "loop" # SyntaxError
# class = 5 # SyntaxError
# Valid - add underscore or change name
if_var = 10
for_loop = "loop"
class_name = 5
You can check the full list of keywords with help("keywords") in the Python interpreter.
Using a keyword as a variable will always raise a SyntaxError.
Rule 5: Use Snake Case for Readability
Python's official style guide, PEP 8, recommends using snake_case for variable names.
This means using lowercase letters and underscores to separate words.
# Good - snake_case
user_age = 25
first_name = "John"
total_price = 99.99
# Avoid - camelCase or PascalCase for variables
# userAge = 25
# FirstName = "John"
Snake case makes variable names easy to read. It is the standard in the Python community.
For more examples, check our Python Variables Examples Guide.
Rule 6: Do Not Use Built-in Function Names
Python has many built-in functions like print, len, type, input, and sum.
You can technically use them as variable names, but you should not. It will overwrite the function.
# Bad practice - overwrites built-in function
print = "hello"
# Now print() no longer works!
# print("test") # TypeError: 'str' object is not callable
# Good practice
print_output = "hello"
print(print_output) # Output: hello
Never use built-in function names as variables. It breaks your code and confuses others.
Rule 7: Variable Names Must Be Unique in Scope
Within the same scope (like inside a function or loop), you cannot have two variables with the same name.
If you do, the second assignment overwrites the first.
x = 10
x = 20 # This overwrites the first x
print(x) # Output: 20
This is not a syntax error, but it can cause logical bugs. Be careful when reusing variable names.
For a deeper look at scope and conflicts, see Python Variable Shadowing and Namespace Conflicts: A Complete Guide.
Rule 8: Use Meaningful Names
Variable names should describe what they store. Avoid single letters except for simple loops.
# Bad - unclear meaning
a = 25
b = "John"
c = 99.99
# Good - clear meaning
user_age = 25
user_name = "John"
product_price = 99.99
Good names make your code self-documenting. Other developers (and future you) will thank you.
Rule 9: Follow PEP 8 Conventions
PEP 8 is the official style guide for Python code. It offers these variable naming rules:
- Use lowercase with underscores for variable names (snake_case).
- Use ALL_CAPS for constants (e.g.,
MAX_SIZE = 100). - Use a leading underscore for internal/private variables (e.g.,
_internal). - Use double leading underscore for name mangling in classes (e.g.,
__private).
# Constants - use ALL_CAPS
PI = 3.14159
MAX_USERS = 1000
# Internal variable - use leading underscore
_internal_data = "hidden"
# Name mangling in classes
class MyClass:
def __init__(self):
self.__private = "secret"
Following PEP 8 makes your code consistent with the wider Python ecosystem.
Rule 10: Avoid Confusing Characters
Some characters can be confusing. For example, the lowercase letter l, uppercase I, and number 1 look similar.
The uppercase O and number 0 also look alike.
# Avoid - confusing characters
l = 1 # Looks like 1
O = 0 # Looks like 0
I = 1 # Looks like l
# Better
length = 1
output = 0
index = 1
Choose clear names. Your code will be easier to read and debug.
Common Mistakes and How to Fix Them
Here are the most common variable naming mistakes beginners make:
Using a Number at the Start
# Error
# 1st_place = "gold" # SyntaxError
# Fix
first_place = "gold"
Using a Hyphen Instead of Underscore
# Error
# user-name = "Alice" # SyntaxError
# Fix
user_name = "Alice"
Using a Keyword
# Error
# class = "math" # SyntaxError
# Fix
class_name = "math"
Forgetting Case Sensitivity
name = "Alice"
print(Name) # NameError: name 'Name' is not defined
NameError: name 'Name' is not defined
Always double-check your spelling and capitalization.
Best Practices Summary
Follow these best practices for clean, readable Python code:
- Use snake_case for variable names.
- Start with a lowercase letter or underscore.
- Use only letters, numbers, and underscores.
- Avoid Python keywords and built-in function names.
- Use meaningful, descriptive names.
- Be consistent with case throughout your code.
- Use ALL_CAPS for constants.
These rules will help you write code that is easy to understand and maintain.
For more on variable types, see Understanding Python Variable Types.
Conclusion
Python variable naming rules are simple but strict. Follow them to avoid errors and write clean code.
Remember the key points: start with a letter or underscore, use only valid characters, avoid keywords, and be consistent with case.
Good variable names make your code readable and professional. Practice these rules in every project.
Now you know the rules. Go write some great Python code.