Every name you invent in a Python program is called an identifier. Variable names, function names, class names, and module names are all identifiers, and Python has a clear set of rules about what makes a valid one. These rules are enforced by the language itself: if you try to use an invalid name, Python raises a SyntaxError before your code even starts running. Beyond the hard rules that Python enforces, there is also a set of community conventions that make Python code consistent and readable across different projects and teams.
Learning the naming rules early prevents a category of errors that can be confusing for beginners. When you see a SyntaxError and you are certain the rest of the line is correct, the problem is often an illegal identifier name. The rules are straightforward, and once you internalize them, you will pick valid names without thinking about it.
The hard rules: what Python enforces
Python identifiers must follow three basic rules, and the interpreter rejects anything that violates them. First, an identifier must start with a letter (uppercase or lowercase A through Z) or an underscore character. It cannot start with a digit. Second, after the first character, an identifier can contain letters, digits, and underscores. No other characters are allowed, including spaces, hyphens, dots, or symbols. Third, an identifier cannot be one of Python's reserved keywords. Using class, for, or if as a variable name produces an immediate error.
These rules are checked at the parser level, which means Python catches invalid names before any of your code runs. Valid names include student_count, private_value, result2, and placeholder_name. Names that break the rules include 2nd_place (starts with a digit), student-count (hyphen is the minus operator), and student count (space splits into two tokens). And class is a reserved keyword that cannot be repurposed. If you need a reference for which words are off-limits, see the full list of Python keywords.
Case sensitivity and Unicode
Python identifiers are case-sensitive, which means the capitalization of a name matters. The names score, Score, and SCORE are three completely different variables as far as Python is concerned. You could assign all three in the same program, and each would hold its own independent value. This gives you flexibility, but it also creates a trap: accidentally writing Score when you meant score creates a new variable instead of referencing the existing one, and Python will not warn you. This is one reason why Python's naming conventions strongly discourage using names that differ only in capitalization.
Python 3 also supports Unicode characters in identifiers, which means you can use letters from non-Latin alphabets. Names with accented characters, Chinese characters, or Cyrillic letters are all technically valid because the characters belong to Unicode categories classified as letters. While Unicode identifiers are legal, the Python community strongly prefers ASCII-only names for code that will be shared, reviewed, or deployed. Sticking to ASCII letters and digits ensures that your code is readable on any system, in any editor, by any developer.
Naming conventions: what the community recommends
Beyond the hard rules, Python has a widely adopted set of style conventions defined in PEP 8, the official Python style guide. These conventions are not enforced by the interpreter, but following them makes your code instantly familiar to other Python developers and helps tools like linters and formatters work consistently.
Variables and functions use snake_case: all lowercase letters with underscores separating words. Examples include student_name, calculate_total, and max_retry_count. Classes use PascalCase (also called CapWords): each word starts with a capital letter and there are no separators. Examples include StudentRecord and ShoppingCart. Constants use UPPER_CASE with underscores: MAX_CONNECTIONS, DEFAULT_TIMEOUT.
These conventions create visual cues that help you understand code at a glance. When you see a PascalCase name like DatabaseConnection, you know it is a class. When you see UPPER_CASE like API_KEY, you know the value is meant to be treated as a constant. Consistency in naming is one of the simplest and most effective ways to write readable code. When you start working with Python variables in detail, these conventions will become second nature.
Underscore prefixes and special meaning
Python assigns special meaning to identifiers that start or end with underscores in certain patterns. A single leading underscore, like internal_value, is a convention that signals the name is intended to be private to a module or class. Python does not enforce this, but tools and IDEs respect it, and the from module import * syntax will not import names starting with an underscore.
A double leading underscore, like __private_value, triggers name mangling inside class definitions, which Python uses to avoid name collisions in subclasses. Names with double underscores on both sides, like init and str, are reserved for Python's own special methods and should never be invented for your own use unless you are intentionally overriding a defined special method.
For now, the important takeaway is simple: avoid inventing names that start or end with double underscores. These are Python's territory. A single trailing underscore, like class_, is sometimes used when you need a name that would otherwise conflict with a keyword, but it is better to choose a different descriptive name than to rely on this workaround.
Rune AI
Key Insights
- Identifiers must start with a letter or underscore, followed by letters, digits, or underscores.
- Python identifiers are case-sensitive: name, Name, and NAME are three different variables.
- You cannot use Python keywords as identifiers.
- Use snake_case for variables, PascalCase for classes, and UPPER_CASE for constants.
- Leading underscores carry special meaning in certain contexts.
Frequently Asked Questions
Can Python variable names contain spaces?
Can I use non-English characters in Python names?
Is there a maximum length for Python identifiers?
Conclusion
Python gives you wide freedom in naming things, but that freedom comes with a responsibility to be clear and consistent. Valid names follow simple technical rules: start with a letter or underscore, continue with letters, digits, or underscores, and avoid keywords. Good names go further: they describe purpose, follow community conventions, and make your code readable to your future self and to anyone else who picks up your file.
More in this topic
Create and Assign Variables in Python
Learn every way to create and assign variables in Python, from basic single assignment to expressions, type hints, and practical patterns beginners use every day.
Python Variable Naming Rules
Learn Python's variable naming rules, including allowed characters, reserved keywords, PEP 8 conventions, and how to choose names that make your code readable.
Python Variables Explained
Understand what Python variables are, how they store and reference data, and why Python's variable model is different from other programming languages.