VMware ESXi and vSphere Cluster Management

Python Variable Names: Rules and Naming Conventions

Learn Python variable-name rules, including valid characters, underscores, keywords, case sensitivity, and readable snake_case conventions.

A variable is a named reference used to store or access a value. The variable name is the identifier you use in your code to refer to that value later.

In an assignment statement, the name appears on the left side of =, and the value appears on the right:

age = 21
name = "Ava"

Here, age and name are variable names. The values assigned to them are 21 and "Ava". The equals sign performs assignment: it binds a name to a value.

What Is an Identifier?

An identifier is a valid name for a program element, such as a variable, function, or class. Python variable names follow identifier rules. A name must be written in a form that Python can interpret as one identifier rather than as several separate pieces of code.

Characters Allowed in Python Variable Names

For beginner Python code, use letters, digits, and underscores. The usual ASCII characters are:

  • Letters from A to Z and a to z
  • Digits from 0 to 9
  • The underscore character, _

Digits are allowed after the first character, but a variable name cannot begin with a digit:

name_1 = "Tuna"
student2 = "Ava"
_temporary_value = 10

Punctuation such as semicolons is not part of an ordinary variable name:

# Invalid
name;_1 = "Tuna"

Python also supports some Unicode characters in identifiers. However, simple ASCII letters, digits, and underscores make names easier for beginners and teams to read, type, and maintain.

The First-Character Rule

The first character of a variable name must be a letter or an underscore. It cannot be a digit.

# Valid
name_1 = "Tuna"
item2 = "Book"

# Invalid
1_name = "Tuna"
2nd_item = "Book"

A name beginning with a digit usually produces a SyntaxError. To fix it, move the number later in the name or add a descriptive word before it:

item_1 = "Book"
second_item = "Book"

Spaces Are Not Allowed in Variable Names

Spaces separate tokens in Python code. They cannot be part of one variable name. Use an underscore to join multiple words:

# Valid
first_name = "Tuna"

# Invalid
first name = "Tuna"

Python reads first name as two separate pieces rather than one identifier. The underscore in first_name keeps the words together as one name.

Keywords and Built-in Names

Python keywords

A keyword is a reserved Python word with a defined language meaning. Keywords control the structure of Python programs, so they cannot be used as variable names.

# Invalid: for is a Python keyword
for = "Tuna"

# Invalid: class is a Python keyword
class = "Beginner"

Choose another name, such as message, loop_item, or class_name, depending on what the value represents.

Built-in function names

A built-in function is a function available by default in Python. Examples include print(), len(), and type().

Built-in names are different from keywords. Python technically allows you to assign a value to a built-in name, but doing so is normally a bad practice because it causes shadowing. Shadowing means replacing access to an existing name with your own variable.

print = "Tuna"

# This now fails because print no longer refers to the built-in function
print("Hello")

Prefer a descriptive alternative:

message = "Tuna"
output_text = "Tuna"
print(message)

Descriptive but Proportionate Names

A good variable name briefly communicates what its value represents. Avoid names that are so abbreviated that their meaning is unclear:

MeaningUnclear nameRecommended nameOverly long name
A person's first namef_nfirst_namefirst_name_of_a_person
A user's email addresseemail_addressemail_address_for_the_current_registered_user
A running totaltrunning_totaltotal_value_accumulated_during_the_current_processing_loop

first_name is clearer than f_n, while the long alternatives repeat information unnecessarily. Choose names that are specific enough to understand without making every detail part of the identifier.

Use snake_case for multiple words

snake_case is a naming style that uses lowercase words separated by underscores. It is the standard style for ordinary multiword variable names in Python.

first_name = "Tuna"
email_address = "tuna@example.test"
shopping_cart_total = 24.50

For ordinary variables, prefer lowercase snake_case rather than spaces, mixed capitalization, or punctuation.

Python Is Case-Sensitive

Python is case-sensitive, which means capitalization changes a name's identity. Names that differ only in capitalization are separate variables:

first_name = "Tuna"
First_Name = "Salmon"

print(first_name)  # Tuna
print(First_Name)  # Salmon

first_name and First_Name do not refer to the same variable. Inconsistent capitalization can cause a NameError or produce an unexpected value. Use consistent lowercase snake_case for ordinary variables.

Python Variable-Name Rules at a Glance

RuleValid exampleInvalid or discouraged exampleReason
Allowed charactersname_1 = "Tuna"name;_1 = "Tuna"Use letters, digits, and underscores; punctuation is not part of the name.
Starting characterstudent2 = "Ava"2nd_student = "Ava"A name must begin with a letter or underscore.
Spacesfirst_name = "Tuna"first name = "Tuna"Spaces separate tokens, so use underscores between words.
Keywordsloop_item = "Tuna"for = "Tuna"Keywords are reserved for Python syntax.
Built-in namesmessage = "Tuna"print = "Tuna"Reusing a built-in name shadows the original function.
Descriptive namingemail_address = "a@example.test"e = "a@example.test"The recommended name communicates the value's purpose.
Case sensitivityfirst_nameFirst_Name when the intended name is first_nameDifferent capitalization creates a different name.

Valid and Invalid Assignment Examples

Review both syntax and readability when choosing a name:

# Valid and readable
user_name = "Ava"
total2 = 42
_temporary_value = 10

# Invalid syntax
2nd_user = "Ava"
user name = "Ava"
user-name = "Ava"
class = "Beginner"

# Technically valid but discouraged
print = "Ava"

total2 is syntactically valid because the digit does not come first. user-name is not one identifier: Python interprets the hyphen as an operator. print can be assigned, but it should normally be avoided because it shadows the built-in function.

Name Review Exercise

Classify each proposed name as valid and readable, invalid, or valid but unsuitable by convention:

  • user_name
  • user name
  • 2nd_user
  • total2
  • class
  • email_address

The classifications are:

  • user_name: valid and readable.
  • user name: invalid because it contains a space.
  • 2nd_user: invalid because it begins with a digit.
  • total2: valid; a digit is allowed after the first character.
  • class: invalid because it is a keyword.
  • email_address: valid and readable.

Troubleshooting Variable-Name Errors

SyntaxError after starting a name with a number

Cause: The proposed name begins with a digit.

Fix: Move the digit later or begin with a letter or underscore:

item_1 = "Book"

SyntaxError after using a space or punctuation

Cause: Python interprets spaces and punctuation as code syntax rather than as part of one identifier.

Fix: Use one name with underscores:

first_name = "Tuna"

SyntaxError after using a keyword

Cause: The name is reserved by Python.

Fix: Choose a descriptive name that is not a keyword, such as loop_item instead of for.

print() fails after assigning to print

Cause: The built-in print function was shadowed by a variable named print.

Fix: Rename the variable and restart the Python session, or remove the shadowing assignment if your environment allows it:

output_text = "Tuna"
print(output_text)

A NameError or unexpected value appears for almost identical names

Cause: Capitalization differs. Python treats each capitalization as a separate name.

Fix: Use consistent lowercase snake_case and reference the exact name that was assigned.

Variable-Name Checklist

  • Does the name begin with a letter or underscore?
  • Does it use only letters, digits, and underscores?
  • Are there no spaces or punctuation characters?
  • Is it different from every Python keyword?
  • Does it avoid shadowing a built-in name such as print?
  • Does it communicate what the value represents?
  • Is it concise rather than cryptic or unnecessarily long?
  • Does it use lowercase snake_case for multiple words?
  • Will you use the exact same capitalization every time?

For a related review of this topic, see Python variable names.