Python Variable Names
A variable name is the label that identifies a variable. Names use only letters, digits, and underscores.
Letters, digits, and underscores
These names are all legal.
user_name = "Kai"
user_age2 = 14
print(user_name)
print(user_age2)Never start with a digit
A digit can appear later in the name.
# 2score = 10 is not allowed
score2 = 10
print(score2)Case makes a new variable
Small and capital letters mean different names.
age = 12
Age = 20
print(age)
print(Age)Keywords are not allowed
The word class is reserved. Add a word to make a legal name.
class_name = "art"
print(class_name)Example
first_name = "Lena"
lucky_number = 7
favorite_color = "green"
print(first_name)
print(lucky_number)
print(favorite_color)Key points
- Use only letters, digits, and underscores in a name
- A variable name identifies one variable in code.
- A name cannot start with a digit
- snake_case: join words with underscores, like first_name
- Names are case sensitive, so age and Age differ
- Keyword: a word Python reserves for its own use
- Use descriptive names so the meaning stays clear
- Readers expect snake_case.
In this example, first_name stores text. The name uses snake_case. Next, lucky_number stores a whole number. Then favorite_color stores another text value. Each print shows one well-named value.
Note
snake_case is a shared habit, not a Python rule.
Python accepts other styles.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
Which one is a legal variable name?