Python identifiers are user-defined names. They are used to specify the names of variables, functions, classes, modules, etc.
Rules to Create Python Identifiers
There are a few rules that must be followed to create a python identifier.
- You can’t use reserved keywords as an identifier name. If you will try, it will throw SyntaxError.
- Python identifier can contain letters in a small case (a-z), upper case (A-Z), digits (0-9), and underscore (_).
- Identifier names can’t begin with a digit. For example, 10test would be an invalid identifier.
- Python identifier can’t contain only digits. For example, 888 would be an invalid identifier.
- Python identifier name 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 small cases for identifiers for uniformity across your programs.
Python Valid Identifiers Example
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, underscore is a valid identifier
- _abc: identifier can start with an underscore
Python Invalid Identifiers Example
- 99: identifier can’t be only digits
- 9abc: identifier can’t start with number
- x+y: the only special character allowed is an underscore
- for: it’s a reserved keyword
How to Test if a String is a Valid Identifier?
We can use 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 if the name is valid or not.
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 if the identifier name is valid or not.
def is_valid_identifier(s): return s.isidentifier() and not keyword.iskeyword(s) print(is_valid_identifier("for")) # False
Python Identifier Naming Best Practices
- Class names should start with capital letters. For example
- If the class name has multiple words, use Uppercase for the first character of each word. For example
- You should use small letters for variables, functions, and module names. For example,
- If variables, functions, and module names have multiple words then separate them with an underscore. For example,
- 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,
- 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.
Identifiers in Python are user-defined names. They are used to define entities in the python program. We should use proper names to hint at the use of the identifier. Follow the rule to “keep it simple and meaningful“.