What are valid python identifiers?



Identifiers are the names used to identify variables, functions, classes, and other objects. A valid identifier helps Python users to assign readable and meaningful names to the elements within the code.

To be considered a valid identifier in Python must follow a specific set of rules.

Rules For Valid Python Identifiers

Following is a list of rules that are to be followed to consider as a valid identifier -

  • The name must begin with the letter (A-Z or a-z) or an underscore.
  • Identifiers cannot be keywords (like if, while, etc).
  • Identifiers are case sensitive.

Using Python str.isidentifier() Method

The Python str.isidentifier() method is a built-in method that is used to check whether the string is a valid identifier according to the Python rules. Following is the syntax for Python str.isidentifier() method -

str.isidentifier()

Example

Let's look at the following example, where we are going to use the str.isidentifier() method and check whether the string is a valid identifier or not.

print("tp123".isidentifier())
print("112hi".isidentifier())
print("for".isidentifier())

The output of the above program is as follows -

True
False
True
Updated on: 2025-06-17T17:34:18+05:30

865 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements