Python Variable Names
3 min read ·
Variable names in Python are used to identify data stored in memory.
Choosing correct and meaningful variable names makes code readable, clean, and professional.
Python follows specific naming rules and conventions that every developer should follow.
Rules for Python Variable Names
Before learning naming styles, understand these basic rules:
- A variable name must start with a letter (a–z, A–Z) or underscore (_)
- It cannot start with a number
- It can contain letters, numbers, and underscores
- Variable names are case-sensitive
- Python keywords cannot be used as variable names
Code Example 1: Valid Variable Names
Code Example 2: Invalid Variable Names
Multi Words Variable Names
When a variable name contains more than one word, it must be written in a readable format.
Python does not allow spaces in variable names.
Instead of:
We use naming conventions.
Key Idea
Multi-word variable names improve clarity and understanding.
Camel Case
In Camel Case, the first word starts with a lowercase letter,
and each new word starts with a capital letter.
Format
Code Example 1: Camel Case
Code Example 2: Camel Case with Numbers
Code Example 3: Camel Case Usage
Usage
Camel Case is commonly used in Java and JavaScript, not preferred in Python.
Pascal Case
In Pascal Case, every word starts with a capital letter, including the first one.
Format
Code Example 1: Pascal Case
Code Example 2: Pascal Case Usage
Code Example 3: Pascal Case in Classes
Best Practice
Pascal Case is mainly used for class names in Python.
Snake Case
In Snake Case, words are separated using underscores (_),
and all letters are usually lowercase.
Format
Code Example 1: Snake Case
Code Example 2: Snake Case with Numbers
Code Example 3: Recommended Style
Python Standard
Snake Case is the recommended naming convention in Python (PEP 8).
Which Naming Style Should You Use in Python?
| Style | Usage in Python |
|---|---|
| Camel Case | ❌ Not recommended |
| Pascal Case | ✅ Class names |
| Snake Case | ✅ Variables and functions |
Learning Insight
Following naming conventions makes your code Pythonic and professional.
Exercise
- Create three variables using Snake Case
- Create a class using Pascal Case
- Identify incorrect variable names and fix them