Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python isidentifier() Method

Python String Isidentifier()

So today in this tutorial, we are going to go through the Python isidentifier() method.

Introduction

Basically, an identifier is a name given to any variable, class, object, function, etc by the user. These names are important to uniquely identify individual variables, classes etc.

Hence, naming is a very important part of any variable, class, function, object, etc declaration. Python restricts the user and provides some basic guidelines for this naming procedure.

Understanding the Python isidentifier() Method

The isidentifier() method checks whether the provided string is eligible to be an identifier or not, and accordingly returns true if it is so, or false if it isn’t.

The syntax for using the Python isidentifier() method is given below.

result = str.isidentifier()

Here,

  • result stores the boolean value (true or false) returned by the method,
  • str is the string for which we need to check whether it is an identifier or not.

Working with the Python isidentifier() Method

Now that we have a basic understanding of the concept of identifiers and the Python isidentifier() method, let us take some examples to understand the working of the method.

string1 = "Askpython"
print(f"Is {string1} a valid identifier? ", string1.isidentifier())

string2 = "i" #an identifier may be of any length > 0
print(f"Is {string2} a valid identifier? ", string2.isidentifier())

string3 = "" #short length not allowed
print(f"Is {string3} a valid identifier? ", string3.isidentifier())

string4 = "_abcd1234" #an identifier may start with an underscore
print(f"Is {string4} a valid identifier? ", string4.isidentifier())

string5 = "1976" #strings starting with numbers are not identifiers
print(f"Is {string5} a valid identifier? ", string5.isidentifier())

Output:

Is Askpython a valid identifier?  True
Is i a valid identifier?  True
Is  a valid identifier?  False
Is _abcd1234 a valid identifier?  True
Is 1976 a valid identifier?  False

Here,

  • For string1 – ‘Askpython’ is a valid identifier as it starts with a character, as well as doesn’t contain any special characters,
  • For string2 – ‘i’ is an valid identifier as it doesn’t contain any special characters as well as is of sufficient length,
  • For string3 – the string doesn’t contain any character hence has a length 0. There should be at least one character inside a string for being eligible as an identifier,
  • For string4 – it is a valid identifier as it starts with an underscore(‘_’) and contains both characters and numbers,
  • For string5 – ‘1976’ is not a valid identifier as it starts with a number.

Conclusion

So that’s it for this tutorial. We learned about the built-in Python isidentifier() method. We highly recommend the readers go through the below reference links. The isidentifier() method is a Python string method.

For any further questions, feel free to contact using the comments below.

References