Excerpt§
Discover the restrictions on variable names in Python and why choosing the right variable names is essential for effective programming. Explore the valid and invalid variable names, specific restrictions in Python, and best practices for selecting meaningful and descriptive names.
Python has specific rules and conventions for naming variables. Using illegal names can cause errors or make your code hard to understand. In this article, we’ll cover what makes a variable name invalid in Python.
Introduction§
In Python, variables hold values or references to objects. You need to assign them unique names adhering to Python’s syntax and style rules.
Proper naming is important because it makes code more readable. Illegal names will generate errors or confuse those reading your code later on.
Syntax Restrictions§
Python has some syntax restrictions on variable names:
No Spaces or Symbols§
Variable names cannot contain spaces, quotation marks, or other symbols like @ and %. The only symbol allowed is the underscore _.
To separate words in a variable name, use underscores. This snake_case style is standard in Python. Tools like String_to_Snake_Case
help format strings into this format.
Length Limit§
Python variable names cannot exceed 253 characters. Excessively long names over this limit can cause issues.
Starts with Letter or Underscore§
The name must begin with an underscore or letter. Starting with a number or other symbol will cause an error.
Avoid Python Keywords§
You cannot use Python keywords and built-in identifiers as variable names. These words are reserved in Python:
1and del from None True
2as elif global nonlocal try
3assert else if not while
4break except import or with
5class False in pass yield
6continue finally is raise
7def for lambda return
python
Tools like String_to_Lowercase
help avoid accidentally using reserved words as names.
Stylistic Conventions§
Python also has some standard naming conventions to follow:
snake_case Over CamelCase§
Variable names should use snake_case
like my_variable
instead of CamelCase
or PascalCase
.
Use String_to_CamelCase When Needed§
However, if interface with other languages that use CamelCase
, you can use tools like String_to_CamelCase
to convert names.
Readability§
Even if a name follows syntax rules, it can still be illegal if it harms readability:
Be Descriptive§
Names should describe the value or purpose of the variable. Generic names like data
or temp
are vague.
Full Words Over Abbreviations§
Avoid short abbreviations that are unclear like cnt
or stmt_cnt
. Write out full words for readability.
Use String_to_Capital Carefully§
You can capitalize the first letter of words with String_to_Capital
to distinguish internal words, like maxStudentCount
. But use this sparingly.
Examples of Illegal Names§
Here are some examples of illegal variable names in Python:
Cannot Contain Spaces§
1my variable = 5 # Illegal, spaces invalid
python
Cannot Begin With Number§
11value = 10 # Illegal, cannot start with number
python
Avoid Weird Symbols§
1my@value = 20 # Illegal, '@' not allowed
python
Only underscores are permitted as symbols in names.
Names Too Short or Long§
1x = 1 # Too short, not descriptive
2
3toolongvariablenameover253chars = 10 # Illegal, exceeds length limit
python
Cannot Use Python Reserved Words§
1for = 5 # Illegal, 'for' is reserved word
python
When Name Mangling Happens§
In Python, name mangling occurs when a name starts with two underscores. This changes it to _classname__variable
to avoid conflicts with subclass names.
It’s best to avoid this double underscore naming to prevent confusion.
Conclusion§
Here are some key points to remember about Python variable name legality:
- Follow
snake_case
conventions for names with underscores - Avoid spaces, symbols besides underscores, and starting with numbers
- Do not use Python keywords or exceed the length limit
- Prioritize readable, descriptive names over abbreviations
- Use
String_to_Uppercase
and other tools to generate valid names
Adhering to Python’s naming rules and conventions prevents illegal variable names and unreadable code. Keep these guidelines in mind as you name variables in your Python programs.