Python Variable Names

When writing Python code, variable names are the foundation of clean, readable, and maintainable scripts. Choosing the right variable names is more than just a good habit—it's essential for professional development and debugging.

In this article, we'll cover:

  • What are variables in Python?
  • Rules for naming variables
  • Best practices for variable naming
  • Valid and invalid variable name examples
  • Common naming conventions in Python

What Is a Variable in Python?

A variable in Python is a name that refers to a value stored in memory. You can think of it as a label attached to a piece of data.

message = "Hello, Python!"
print(message)

Here, message is a variable that stores the string "Hello, Python!".

Rules for Naming Variables in Python

Python has specific rules when it comes to naming variables:

  1. Variable names must start with a letter (a-z, A-Z) or an underscore _.
  2. Subsequent characters can include letters, digits (0-9), or underscores.
  3. Variable names are case-sensitive (name and Name are different).
  4. Cannot use Python keywords (e.g., class, if, else, True, etc.).

Valid variable names:

username = "john_doe"
user1 = "Jane"
_user = "Hidden"
first_name = "Alice"

Invalid variable names:

1user = "Invalid"      # Starts with a digit
user-name = "Error"    # Hyphen is not allowed
class = "Python"       # 'class' is a reserved keyword

✅ Best Practices for Naming Variables

Follow these best practices to make your code clean and professional:

1. Use Descriptive Names

Avoid single letters or vague names.

# Bad
x = 100

# Good
user_score = 100

2. Use Snake Case for Variable Names

Python recommends using snake_case (lowercase words separated by underscores).

total_price = 49.99

3. Avoid Reserved Keywords

Don’t name variables after Python’s built-in functions or keywords.

# Bad
list = [1, 2, 3]  # Overwrites the built-in list function

# Good
number_list = [1, 2, 3]

Common Python Naming Conventions

StyleUsage ExampleDescription
snake_caseuser_emailStandard for variables and functions
camelCaseuserEmailNot common in Python (used in JavaScript)
UPPER_CASEMAX_RETRIESFor constants
_underscore_private_varIndicates a variable is meant for internal use
dunder__init__Special methods in Python (double underscore)

 

Variable Naming Examples in Action

Here’s a simple example showing good variable naming:

first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name

print("Full Name:", full_name)

Output:

Full Name: John Doe

Conclusion

Choosing the right Python variable names improves code readability and reduces errors. Always follow naming rules and best practices to write Pythonic code.

✨ Key Takeaways:

  • Variable names must start with a letter or underscore.
  • Use meaningful, descriptive names in snake_case.
  • Avoid Python keywords and built-in names.
  • Follow PEP 8 naming conventions.

By following these guidelines, you'll write cleaner and more maintainable Python code that others (and future you) will thank you for!