Python Variable Names (Naming Rules and Conventions)
Updated 6 min read Aimerscrew
You already know how to create a variable. Now comes the part that makes your code readable or confusing: choosing good Python variable names. Some naming rules are strict, and breaking them causes an error. Others are conventions that every Python developer follows. In this lesson, you'll learn both, plus a sneaky naming bug that catches almost every beginner.
🌍 Real-life example
Picture a shared kitchen fridge full of plastic containers. One is labeled "Leftover pasta, Tue". Another just says "Stuff". Which one would you trust? Variable names are labels for your data. A clear label saves everyone time; a vague one leads to surprises.
What are the rules for Python variable names?
These rules are enforced by Python itself. Break one, and your program won't run.
- A name can contain letters (a–z, A–Z), digits (0–9) and the underscore
_. - A name cannot start with a digit.
player1is fine;1playeris not. - A name cannot contain spaces, hyphens or symbols like
$,@or!. - Names are case-sensitive:
city,CityandCITYare three different variables. - A name cannot be a keyword, a word Python reserves for its own grammar, like
iforclass.
user_name = "maya_k"
total2 = 150
_count = 0
print(user_name, total2, _count)
maya_k 150 0
What happens if you use an invalid name?
Python refuses to run the file and shows a SyntaxError. Here's what each broken rule looks like.
Starting with a digit
2nd_place = "Leo"
SyntaxError: invalid decimal literal
Python sees the 2 and expects a number, then gets confused by the letters after it.
Using a hyphen
first-name = "Ana"
SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
A hyphen is the minus sign, so Python reads this as "first minus name", which you can't assign to.
Using a space
my score = 10
SyntaxError: invalid syntax
Using a keyword
class = "Math 101"
SyntaxError: invalid syntax
How do you check if a word is a Python keyword?
Python 3.12 has 35 keywords. You don't need to memorize them. The built-in keyword module can check for you:
import keyword
print(keyword.iskeyword("class"))
print(keyword.iskeyword("score"))
print(len(keyword.kwlist))
print(keyword.kwlist[:5])
True
False
35
['False', 'None', 'True', 'and', 'as']
The full list includes words like if, else, for, while, def, return, import, in, is, not, or, pass and with. Run print(keyword.kwlist) to see them all. Most editors also color keywords differently, which is a quick visual warning.
Is Python case-sensitive with variable names?
Yes. Every difference in capitalization creates a separate variable:
city = "Rome"
City = "Oslo"
print(city, City)
Rome Oslo
This is legal, but please don't do it on purpose. Two names that differ only in capitals are an easy way to confuse yourself.
Naming styles: snake_case, camelCase and more
When a name has several words, you need a way to join them, since spaces aren't allowed. There are a few common styles:
| Style | Example | Used in Python for |
|---|---|---|
| snake_case | order_total | Variables and functions (the standard) |
| UPPER_CASE | MAX_UPLOAD_SIZE | Constants, values that shouldn't change |
| PascalCase | ShoppingCart | Class names (you'll meet classes later) |
| camelCase | orderTotal | Rare in Python; common in JavaScript and Java |
The official style guide, PEP 8, recommends snake_case for variable names: all lowercase, with underscores between words. Python won't complain about camelCase, but your code will look out of place next to everyone else's.
MAX_TICKETS = 6 # constant
tickets_requested = 4 # variable in snake_case
print(tickets_requested <= MAX_TICKETS)
True
How do you choose good variable names?
A good name tells the reader what the value means, without needing a comment. Compare these:
| Vague name | Descriptive name | Why it's better |
|---|---|---|
x | account_balance | Says exactly what's stored |
t | temperature_c | Includes the unit (Celsius) |
data | playlist_songs | "data" could mean anything |
flag | is_logged_in | Reads like a yes/no question |
usr_nm | user_name | No guessing at abbreviations |
💡 Tip
Start yes/no variables with is_, has_ or can_, like is_premium or has_coupon. Then lines such as if has_coupon: read almost like English.
Long names aren't bad, but don't go overboard. total_price is better than both tp and the_total_price_of_all_items_in_the_cart. Also avoid single letters that look like numbers: lowercase l, uppercase O and uppercase I.
Why you shouldn't name a variable list, str or sum
Python has built-in functions such as print, input, list, str, int, sum, max and type. They are not keywords, so Python lets you reuse their names. But if you do, your variable shadows (hides) the built-in function, and the original stops working.
sum = 10 + 20
scores = [8, 9, 10]
print(sum(scores))
TypeError: 'int' object is not callable
On line 1, sum became the number 30. On line 3, you tried to use it as the built-in sum() function, but now it's just a number, and you can't "call" a number. Here's the same bug with list:
list = ["eggs", "milk"]
numbers = list(range(3))
TypeError: 'list' object is not callable
The fix is to pick a more specific name, like total or groceries. It's also a better name anyway.
⚠️ Watch out
This bug is confusing because the error appears on a later line, not where you made the mistake. If you see "object is not callable", check whether you reused a built-in name earlier in your code.
What does a single underscore _ mean?
By convention, a lone underscore _ is used for a value you have to receive but don't plan to use. It tells readers "ignore this one".
for _ in range(3):
print("Hip hip hooray!")
Hip hip hooray!
Hip hip hooray!
Hip hip hooray!
The loop needs a variable, but we never use it, so _ is perfect. It's also handy when unpacking values:
name, _, city = "Ava", 29, "Boston"
print(name, city)
Ava Boston
Names that start with an underscore, like _cache, have their own meaning in larger programs ("for internal use"). You'll see that when you learn about modules and classes.
Common mistakes with variable names
- Starting with a number, like
3d_model. Usemodel_3dinstead. - Using hyphens as in
zip-code. Usezip_code. - Shadowing built-ins such as
list,str,input,sumormax. - Typos in capitalization: creating
Totaland later usingtotalgives aNameError. - Meaningless names like
a,temp2orstuffthat force readers to guess.
Practice exercise
🏋️ Try it yourself
This bank-account snippet won't run, and its names are hard to read. Fix every invalid name, rename the vague ones in snake_case, and avoid shadowing built-ins.
1st-deposit = 200
b = 50
sum = 1st-deposit + b
print(sum)
Show solution
first_deposit = 200
second_deposit = 50
account_balance = first_deposit + second_deposit
print(account_balance)
250
1st-deposit broke two rules (starts with a digit, contains a hyphen), b said nothing about its value, and sum hid the built-in sum() function.
What's next?
Your variables now have clear, valid names. Next, explore the different kinds of values they can hold in Python data types.
Key takeaways
- Variable names may use letters, digits and underscores, but cannot start with a digit.
- Spaces, hyphens and symbols like $ are not allowed, and names are case-sensitive.
- Keywords such as if, class and for cannot be names; check with keyword.iskeyword().
- PEP 8 recommends snake_case for variables, UPPER_CASE for constants and PascalCase for classes.
- Pick descriptive names like account_balance instead of x or data.
- Never reuse built-in names such as list, str, sum or input; it breaks those functions.
- Use a single underscore _ for values you need to receive but will not use.
Frequently asked questions
Can a Python variable name start with a number?
No. A name must start with a letter or an underscore. Put the digit later instead, for example player1 instead of 1player.
Should I use snake_case or camelCase in Python?
Use snake_case for variables and functions. It is the style recommended by PEP 8 and used by almost all Python code. camelCase works but looks out of place.
Can I use a hyphen in a Python variable name?
No. Python reads a hyphen as the minus operator. Use an underscore instead, such as first_name.
Why does Python say "object is not callable"?
Often it means you stored a value in a variable with the same name as a built-in function, such as sum or list, and then tried to call that function. Rename your variable.
How long can a Python variable name be?
There is no practical length limit. Aim for a name that is short but still clearly describes the value, such as total_price.