….
🔤 Python Variable Naming – Best Practices for Clean & Readable Code
💡 What Are Variable Names in Python?
In Python, variables are identifiers that store values. Choosing the right variable name is crucial for readability, maintainability, and debugging. Whether you’re writing a simple script or building an entire application, well-named variables improve code quality dramatically.
✅ Rules for Naming Python Variables
Python variable names follow specific rules. Breaking these rules will result in syntax errors.
✅ Legal variable names:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
❌ Illegal variable names:
2myvar = "John" # Starts with a number
my-var = "John" # Contains a hyphen
my var = "John" # Contains a space
🔒 Python Variable Naming Rules Summary
- Must start with a letter (a–z, A–Z) or an underscore (_)
- Cannot start with a number
- Can only contain letters, digits, and underscores
- Are case-sensitive (
MyVar
,myvar
, andMYVAR
are different) - Cannot use Python reserved keywords like
if
,while
,class
, etc.
Use this quick reference to stay on track!
🔡 Multi-Word Variable Naming Styles
When your variable needs to be more descriptive (e.g., totalMarks
, studentScore
, etc.), here are 3 standard naming conventions used in Python:
1. Camel Case
Each word, except the first, starts with a capital letter.
studentName = "Alice"
totalMarks = 95
2. Pascal Case
Each word starts with a capital letter (often used in class names).
StudentName = "Alice"
TotalMarks = 95
3. Snake Case (Recommended in Python) 🐍
Words are separated by underscores. This is the preferred style in Python for variable names and function names.
student_name = "Alice"
total_marks = 95
💬 GoNimbus Tips:
- ✅ Always choose meaningful and descriptive variable names.
- 🚫 Avoid cryptic names like
x1
,a
,temp123
unless necessary. - 🎯 Follow PEP 8 – Python’s official style guide, which recommends snake_case for variables.
🧪 Try It Yourself!
Use our GoNimbus Python Editor to practice declaring and using variables with different naming conventions:
first_name = "John"
last_name = "Doe"
age = 25
print(first_name, last_name, age)
💥 Instant output. Live syntax highlighting. Beginner-friendly!