- Valid variable names and naming rules in Python
- Valid characters for identifiers (= names)
- ASCII
- Unicode
- Check if the string is a valid identifier: isidentifier()
- Words that cannot be used as identifiers: Reserved words and keywords
- Words that should not be used as identifiers
- Naming conventions (PEP8)
- Identifiers in Python – Rules, Examples & Best Practices
- Rules for Writing Identifiers in Python
- Examples of Valid Identifiers in Python
- Examples of Invalid Valid Identifiers in Python
- How to Test if a String is a Valid Identifier?
- Best Practices for Naming Identifiers in Python
- Summary
- What’s Next?
- What are Identifiers in Python?
- Table of contents
- Rules for writing Identifiers in Python
- Python Valid Identifiers Example
- Python Invalid Identifiers Example
- Python Keywords
- Testing the Validity of Python Identifiers
- Input:
- Output:
- Input:
- Output:
- Python Identifier Naming Best Practices
- Closing Thoughts on Python identifiers
Valid variable names and naming rules in Python
In Python, identifiers (= variable names, function names, class names, etc.) need to be defined according to rules. Names that do not follow the rules cannot be used as identifiers.
The following is for Python 3 and may be different for Python 2.
Valid characters for identifiers (= names)
Here, the characters that can and cannot be used for identifiers (= variable names, function names, class names, etc.) are shown.
Basically, just remember the following.
- Uppercase and lowercase alphabets, numbers, and the underscore ( _ ) can be used.
- Numbers cannot be used as the first character.
Although non-alphabetic characters like Kanji can be used, it’s recommended to avoid them unless there is a compelling reason.
ASCII
ASCII characters that can be used for identifiers are uppercase and lowercase alphabets ( A — Z , a — z ), numbers ( 0 — 9 ), and the underscore ( _ ).
AbcDef_123 = 100 print(AbcDef_123) # 100
Symbols other than the underscore cannot be used.
# AbcDef-123 = 100 # SyntaxError: can't assign to operator
Numbers cannot be used as the first letter.
# 1_abc = 100 # SyntaxError: invalid token
The underscore can be used for the first letter.
Note that the underscore at the beginning may have a special meaning.
Unicode
In Python 3, Unicode characters such as Kanji and Hiragana can be used.
変数その1 = 100 print(変数その1) # 100
Not all Unicode characters can be used. For example, you cannot use emoji.
# ☺ = 100 # SyntaxError: invalid character in identifier
See the official documentation for the Unicode category codes that can be used.
Check if the string is a valid identifier: isidentifier()
You can check whether a string is a valid identifier with the isidentifier() method of the string ( str ).
It returns True if the string is a valid identifier and False if not.
print('AbcDef_123'.isidentifier()) # True print('AbcDef-123'.isidentifier()) # False print('変数その1'.isidentifier()) # True print('☺'.isidentifier()) # False
Words that cannot be used as identifiers: Reserved words and keywords
Reserved words and keywords are valid as identifiers but cannot be used as ordinary identifiers.
Note that isidentifier() returns True for reserved words and keywords since they are valid strings. However, using them as identifiers (variable names, function names, class names, etc.) will raise an error.
print('None'.isidentifier()) # True # None = 100 # SyntaxError: can't assign to keyword
To get a list of keywords and check if a string is a keyword, use the keyword module of the standard library. See the following article.
Words that should not be used as identifiers
The names of built-in functions can be used as identifiers, so you can assign new values to them.
For example, len() is a built-in function that returns the number of elements in a list or the number of characters in a string.
If you assign a new value to the name len , the original function is overwritten. Note that no error or warning is printed when assigning.
print(len('abc')) # 3 len = 100 print(len) # 100 # print(len('abc')) # TypeError: 'int' object is not callable
A common mistake is writing list = [0, 1, 2] , which prevents the use of list() .
For more information on checking the list of built-in functions and constants, see the following article.
Naming conventions (PEP8)
PEP stands for Python Enhancement Proposal.
PEP stands for Python Enhancement Proposal. A PEP is a design document providing information to the Python community, or describing a new feature for Python or its processes or environment.
PEP 1 – PEP Purpose and Guidelines | peps.python.org
PEP8 describes the «Style Guide for Python Code».
Naming conventions are also mentioned.
For example, the following styles are recommended. See the link above for details.
- Module names
- lowercase_underscore
- lowercase
- CapitalizedWords ( CamelCase )
- lowercase_underscore
- ALL_CAPS
If your organization does not have its own naming conventions, it is recommended to follow PEP8.
Identifiers in Python – Rules, Examples & Best Practices
An Identifier is a user-defined name given to variables, functions, classes, modules, and other objects. As simple as it sounds, it is not. An identifier is not just any name, it helps to make code more readable and easier to understand by giving things relevant names. For this many rules have to be followed.
In this tutorial, we will learn the rules for writing identifiers, examples of valid and invalid identifiers, how to test whether a string is a valid identifier, and finally, we will understand best practices for naming identifiers.
Rules for Writing Identifiers in Python
A few naming conventions and rules must be followed when writing a Python identifier.
- Keywords cannot be used as identifier names as they are reserved words in Python programming language. If you try, it will throw SyntaxError.
- Python identifiers can contain letters in a small case (a-z), upper case (A-Z), digits (0-9), and underscore (_).
- Identifiers cannot begin with a digit. For example, 10test would be an invalid identifier.
- Python identifiers can’t contain only digits. For example, 888 would be an invalid identifier.
- Python identifier names can start with an underscore. So, the _test would be a valid identifier.
- There is no limit on the length of the identifier name. But, don’t try to keep a super long identifier, it will only hurt your credibility as a programmer.
- Python identifier names are case-sensitive. So, “abc” and “ABC” are two different identifiers. It’s best to use lowercase letters for identifiers for uniformity across your programs.
Examples of Valid Identifiers in Python
Let’s look at some examples of valid identifiers in Python.
- ab10c: Contains only letters and numbers
- abc_DE: Contains all the valid characters
- _: Surprisingly but yes, an underscore is a valid identifier
- _abc: Identifier can start with an underscore
Examples of Invalid Valid Identifiers in Python
Let us now look at some examples of invalid identifiers in Python.
- 99: Identifier can’t be only digits
- 9abc: Identifier can’t start with the number
- x+y: The only special character allowed is an underscore
- for: It’s one of the reserved keywords in Python
How to Test if a String is a Valid Identifier?
We can use the string isidentifier() function to check if the identifier name is valid or not. But, this method doesn’t take reserved keywords into consideration. So, we can use this function with keyword.iskeyword() to check whether the name is valid.
print("abc".isidentifier()) # True print("99a".isidentifier()) # False print("_".isidentifier()) # True print("for".isidentifier()) # True - wrong output
We know that “for” is a reserved keyword. So it’s not a valid identifier. Let’s define a function to test whether the identifier name is valid.
def is_valid_identifier(s): return s.isidentifier() and not keyword.iskeyword(s) print(is_valid_identifier("for")) # False
Best Practices for Naming Identifiers in Python
- Class names should start with capital letters. For example Person, Employee, etc.
- If the class name has multiple words, use Uppercase for the first character of each word. For example EmployeeData, StringUtils, etc.
- You should use small letters for variables, functions, and module names. For example, collections, foo(), etc.
- If variables, functions, and module names have multiple words then separate them with an underscore. For example, is_empty(), employee_object, etc.
- For private variables, you can start their names with an underscore.
- Avoid underscore as the first and last character in the identifier name. It’s used by Python built-in types.
- If the identifier starts and ends with two underscores, then it means that the identifier is a language-defined special name, such as __init__. So you should avoid having two underscores at the start and the end of the identifier name.
- Keep identifier names meaningful to clarify their intent. For example, phone_number, is_uppercase, etc.
- If a function returns a boolean value, it’s better to start its name with “is”. For example, isidentifier, iskeyword, etc.
- There is no limit on the length of the identifier name. But, keep it small and to the point. For example, the_employee_object_first_name can be better named as emp_first_name.
Summary
Keywords and identifiers are two essential concepts used in Python. A Python identifier is a name used as a variable name to enhance the readability and understanding of the code. In this tutorial, we have learned the rules of naming identifiers with examples of valid and invalid identifiers, which you must definitely keep in mind while writing identifiers on your program.
What’s Next?
What are Identifiers in Python?
In this tutorial, we will answer the question — what are Identifiers in Python. An identifier is a user-defined name given to identities like class, functions, variables, modules, or any other object in Python. Here we will discuss the rules for writing identifiers.
Table of contents
Rules for writing Identifiers in Python
- The identifier is a combination of character digits and underscore and the character includes letters in lowercase (a-z), letters in uppercase (A-Z), digits (0-9), and an underscore (_).
- An identifier cannot begin with a digit. If an identifier starts with a digit, it will give a Syntax error.
- In Python, keywords are the reserved names that are built-in to Python, so a keyword cannot be used as an identifier — they have a special meaning and we cannot use them as identifier names.
- Special symbols like !, @, #, $, %, etc. are not allowed in identifiers.
- Python identifiers cannot only contain digits.
- There is no restriction on the length of identifiers.
- Identifier names are case-sensitive.
Python Valid Identifiers Example
Python Invalid Identifiers Example
Python Keywords
Here is the list of some reserved keywords in Python that cannot be used as identifiers.
False def if raise None del import return True elif in try and else is while as except lambda with assert finally nonlocal yield break for not await class form or async continue global pass As you can see here all the keywords except ‘True’, ‘False’, and ‘None’ are in lowercase, therefore they must be written as they are.
Testing the Validity of Python Identifiers
The str.isidentifier() function is used to check the validity of an identifier but, this method doesn’t take reserved keywords into consideration. So, we can use this function with keyword.iskeyword() to check if the name is valid or not.
Input:
print ("abc".isidentifier()) print ("123abc".isidentifier()) print ("_abc".isidentifier()) print ("for".isidentifier())
Output:
Now, we know “for” is a reserved keyword so, it is an invalid identifier.
Input:
def is_valid_identifier(x): Return x.isidentifier() and not keyword.iskeyword(x) print(is_valid_identifier("for"))
Output:
Python Identifier Naming Best Practices
- Only class names are started with capital letters (Student, Employee).
- Multiple words in a variable are separated by an underscore (is_valid()).
- If an identifier begins with an underscore, it means it is a private identifier. This does not make the variable private. It is only for the ease of the programmer to easily distinguish between private variables and public variables.
- Python built-in magic methods use double underscores around the names (__len__). Therefore, double underscores are used only when you are dealing with mangling in Python.
- Using longer name than one character is always preferred (index = 1 is better than i = 1).
- Camel case are used while naming the variables (studentAddress, studentId). Camel case use a capital letter to begin the second word in a compound name or phrase, when it is not separated from the first word by a space.
Closing Thoughts on Python identifiers
Python Identifiers are user-defined names. We discussed the rules and some best practices while naming identifiers that are good for fellow Python programmers. One can learn about more Python concepts here.