Python Variable Naming Rules and Conventions
Learn Python variable name syntax, keywords, case sensitivity, snake_case, built-in shadowing, and practical valid-versus-invalid examples.
A variable name is an identifier: a name that lets your program refer to a stored value. In Python, assignment binds a name to a value with the = operator.
first_name = 'Tuna'
item_count = 3
In first_name = 'Tuna', first_name is the identifier, = is the assignment operator, and 'Tuna' is the value. The name can be used later to access that value.
Variable names follow syntax rules. Python also has naming conventions that make code easier to read and maintain. A convention is a recommendation, not a grammar requirement.
Python variable name rules at a glance
| Rule | Valid example | Invalid or discouraged example | Reason |
|---|---|---|---|
| Permitted characters | name_1 | name;_1 | Use letters, digits, and underscores. Punctuation such as semicolons is not part of an ordinary identifier. |
| No initial digit | name_1 | 1_name | An identifier cannot begin with a number. |
| No spaces | first_name | first name | A space separates Python syntax elements instead of joining words into one name. |
| No reserved keyword | class_name | class | Keywords have special meaning in Python syntax. |
| Avoid shadowing built-ins | message | print | Reusing a built-in name can hide useful Python functionality. |
| Case sensitivity | first_name and First_Name are separate names | Inconsistent capitalization | Uppercase and lowercase letters are distinct. |
| Descriptive snake_case | first_name | f_n | Clear, concise names communicate purpose. |
Allowed characters in identifiers
Ordinary Python variable names may contain alphabetic characters, digits, and underscores. An underscore is the character _; it is commonly used to separate words.
name_1 = 'Tuna'
user_age = 21
order_total_2026 = 49.95
Punctuation is not interchangeable with an underscore. For example, a semicolon cannot appear as part of an identifier. A hyphen is also not a valid name character because Python interprets it as the subtraction operator.
name;_1 = 'Tuna' # SyntaxError
user-name = 'Tuna' # SyntaxError: interpreted as user - name
The starting-character rule
A variable name must begin with a letter or an underscore. It must not begin with a digit. Digits are allowed after the first character.
name_1 = 'Tuna' # valid
_name = 'Tuna' # valid
name1 = 'Tuna' # valid
1_name = 'Tuna' # SyntaxError
Although a leading underscore is syntactically valid, names beginning with an underscore can have special conventions in larger programs. For ordinary beginner variables, a clear lowercase name such as user_name is usually easiest to understand.
Spaces and multiword names
Spaces cannot be part of one variable identifier. Python reads space-separated text as separate syntax elements, so first name is not one name. Python may interpret it as two expressions or report a syntax error when it appears in an assignment.
first name = 'Tuna' # SyntaxError
first_name = 'Tuna' # valid
Use snake_case for multiword names. Snake_case uses lowercase words joined by underscores, with no spaces.
Python keywords
A keyword is a reserved word with a special meaning in Python grammar. A keyword cannot be used as the target of an assignment.
class = 'Tuna' # SyntaxError
Common keywords include if, class, for, while, def, return, True, False, and None. Choose an alternative such as class_name, item_count, or is_ready.
The exact keyword list depends on the Python version. Use the keyword module to inspect it:
import keyword
print(keyword.kwlist)
You can test whether a particular string is a keyword:
import keyword
print(keyword.iskeyword('class')) # True
print(keyword.iskeyword('class_name')) # False
Checking whether text can be an identifier
Strings have an isidentifier() method that checks whether their characters and starting character follow identifier rules.
print('first_name'.isidentifier()) # True
print('1_name'.isidentifier()) # False
print('first name'.isidentifier()) # False
isidentifier() is not a complete variable-name test because a keyword can have identifier-like characters. Check both conditions when needed:
import keyword
candidate = 'class'
valid_name = candidate.isidentifier() and not keyword.iskeyword(candidate)
print(valid_name) # False
Built-in names and shadowing
A built-in is a name supplied by Python, such as print, list, str, id, type, or sum. Unlike keywords, built-in names are usually technically permitted as variable names.
Shadowing means assigning a new value to a name that Python would otherwise find in an outer scope or among the built-ins. The assignment below is accepted, but it hides the normal print() function:
print = 'Tuna'
print('Hello') # TypeError: a string is not callable
The name print now refers to a string, not a function. Similar problems can result from assigning to list, str, id, type, or sum.
list = [1, 2, 3]
list('abc') # TypeError, because list now refers to a list
Avoid these names for variables. If shadowing has already happened, rename the variable and restart the interpreter, or remove the name in an interactive session:
del print
Whether del restores access can depend on other assignments or scopes; restarting the interpreter is often the simplest beginner-friendly fix.
Case sensitivity
Python is case-sensitive: uppercase and lowercase characters are treated as different characters. Names that differ only in capitalization refer to separate identifiers.
first_name = 'Tuna'
First_Name = 'Salmon'
print(first_name) # Tuna
print(First_Name) # Salmon
Although this is valid, similar names can confuse readers. Use consistent lowercase spelling for ordinary variables and functions.
Choosing descriptive names
A good name communicates what a value represents. Prefer a clear, concise name over an abbreviation that requires guesswork.
first_name = 'Tuna' # preferred
f_n = 'Tuna' # less clear
Do not make names so short that their meaning disappears, but also avoid unnecessary detail that makes code difficult to scan.
first_name = 'Tuna' # concise and clear
first_name_of_a_person = 'Tuna' # unnecessarily verbose
Descriptive names improve readability because someone can understand the code without tracing every value immediately. They also make maintenance safer: a later change is less likely to misuse a value whose purpose is obvious.
Python naming conventions
Python style commonly uses snake_case for variable and function names:
customer_name = 'Tuna'
def calculate_total(price, tax_rate):
return price + price * tax_rate
Constants—values that a program intends not to change—are commonly written in UPPER_CASE with underscores:
MAX_RETRIES = 3
DEFAULT_TIMEOUT = 10
Uppercase does not make a value immutable. It is a signal to other programmers that the name should be treated as a constant.
| Category | Requirement or recommendation | What happens if ignored |
|---|---|---|
| Identifier syntax | Use letters, digits after the first character, and underscores. | Python reports a SyntaxError for invalid forms. |
| Keywords | Never assign to reserved words such as for or class. | Python rejects the statement with a SyntaxError. |
| Built-in names | Avoid names such as print, list, and sum. | Built-in functionality can be hidden, causing later errors. |
| snake_case | Use lowercase words separated by underscores. | The code may still run, but its style becomes less consistent. |
| Descriptive naming | Choose concise names that explain a value's purpose. | Readers spend more time interpreting and maintaining the code. |
| Uppercase constants | Use UPPER_CASE for values intended to remain unchanged. | The code still runs, but the intended constant-like meaning is less visible. |
Valid and invalid identifier diagnosis
Some naming problems violate Python syntax and stop the program before it runs. Others are valid syntax but poor practice.
# Valid
name_1 = 'Tuna'
first_name = 'Tuna'
item_count2 = 2
# Invalid syntax
1_name = 'Tuna'
first name = 'Tuna'
class = 'Tuna'
user-name = 'Tuna'
print = 'Tuna' and f_n = 'Tuna' are different kinds of problems: they are syntactically valid, but one shadows a built-in and the other is unclear. A linter or code review may flag them even though Python accepts them.
Troubleshooting naming errors
SyntaxError after writing a name with a space
Python reads the separated words as separate tokens rather than one identifier. Replace the space with an underscore, such as first_name.
SyntaxError when assigning to for or class
The selected name is a reserved keyword. Use a descriptive alternative such as item_count or class_name.
TypeError when calling print(), list(), or another built-in
A variable probably reused the built-in function's name. Rename the variable and restart the interpreter, or delete the shadowing name if appropriate.
NameError or an unexpected value from nearly identical names
Check capitalization and spelling. For example, first_name and First_Name are different identifiers. Use one consistent form.
SyntaxError after using a hyphen
Python interprets the hyphen as subtraction. Use an underscore, such as order_total, instead of order-total.
Quick naming checklist
- Does the name begin with a letter or underscore rather than a digit?
- Does it contain only appropriate identifier characters?
- Did you replace spaces and hyphens with underscores?
- Is it different from Python's reserved keywords?
- Did you avoid shadowing names such as
print,list,str,id,type, andsum? - Does it use consistent lowercase
snake_case? - Does it describe the value without being vague or unnecessarily long?
For background, see what variables are, assignment operators, and variable scopes. To study error messages further, review syntax and logical errors and types of errors.