Python identifiers are user-defined names. They are used to specify the names of variables, functions, class, module, etc.
Rules to Create Python Identifiers
There are few rules that must be followed to create a python identifier.
- You can’t use reserved keywords as an identifier name.
- Python identifier can contain letters in a small case (a-z), upper case (A-Z), digits (0-9), and underscore (_).
- Identifier name can’t begin with a digit.
- Python identifier can’t contain only digits.
- Python identifier name can start with an underscore.
- There is no limit on the length of the identifier name.
- Python identifier names are case sensitive.
Python Valid Identifiers Example
Let’s look at some examples of valid python identifiers.
- 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. 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,
- If a function returns 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.
Python identifiers are user-defined names. They are used to define entities in the python program. We should use proper names to hint the use of the identifier. Follow the rule to “keep it simple and meaningful”.