Methods and Functions

Python String isidentifier()

In Python, what exactly is an identifier? An identifier is a name given by the user to any variable, class, object, function, and so on. These names are necessary for uniquely identifying individual variables, classes, and so on. We utilize the Python isidentifier() string class method to ensure that all of the string input characters are identifiers that can be used as names of functions, classes, or variables. This article focuses on Python isidentifier() syntax, its rules, along with real-life applications and examples.

Python isidentifier() function

Definition

  • Python isidentifier() function is used to check and return value TRUE if the input string is a valid identifier else return value FALSE.
  • If the string is a legitimate identifier according to the programming language definition, then the Python isidentifier() method returns TRUE; otherwise, it returns FALSE.

Python Identifiers and Rules to be followed

  • An identifier must begin with a letter of the alphabet.
  • An identifier cannot begin with a number.
  • A space, comma(,), or even a dot(.) must not appear in an identifier.
  • An underscore (_) can be used to separate multiple-word identifiers.
  • An identifier could be a combination of lowercase (a-z), uppercase (A-Z), numerals (0-9), or an underscore (_).
  • A unique identifier can be of any length.
  • Special characters such as #,@,&, and percent are not permitted in an identifier.
  • Because Python is a case-sensitive language, identifiers such as “name“ differ from “Name”.
  • Python keywords, also known as reserved words, should never be used as identifiers.

The following Python keywords are reserved and should not be used as identifiers:

True, False, None, break, continue, pass, import, class, if, else, elif, and , or, for, lambda, in, is, try, while, return, as, assert, async, await, def, del, global, not, nonlocal, with, yield.

isidentifier() Syntax

Python isidentifier() follows the below-mentioned syntax:

                    

string.isidentifier()

isidentifier() Parameters

The isidentifier() string method does not accept any parameter. If any parameter is passed, the function throws an exception.

Return value from isidentifier()

The Python isidentifier() returns a Boolean value:

  • True – if the string contains all valid identifiers
  • False – if the string does not contain at least one valid identifier

Note – If the string starts with a number, or contains whitespaces/special characters; then the isidentifier() function will always return a False value.

Example 1: How isidentifier() works?

Now that we have a basic knowledge of the concept of identifiers and the Python isidentifier() method, let us look at some instances to see how it works.

Example

                    

# Python program to illustrate isidentifier()
str1 = 'HelloWorld'
print(str1.isidentifier())

# letters with space
str2 = 'Python Programming'
print(str2.isidentifier())

# words separated by underscore
str3 = 'Good_Morning_Folks'
print(str3.isidentifier())

# letters and numbers
str4 = 'Dollars100'
print(str4.isidentifier())
 
# string starting with number
str5 = '100_Dollars'
print(str5.isidentifier())

str6 = '@@ 123 Hello'
print(str6.isidentifier())

Output

                    

True
False
True
True
False
False

If the string does not begin with a number (0-9), the Python isidentifier() method will return True. If there is an underscore, regardless of its position, it will return True.

Example 2: More Example of isidentifier()

Example

                    

# Python program to illustrate isidentifier()
str1 = 'Hello_World_100'
if(str1.isidentifier()) == True:
    print(str1, 'is a valid identifier')
else:
    print(str1, 'is not a valid identifier')

str2 = '101 Python Programming Hacks'
if(str2.isidentifier()) == True:
    print(str2, 'is a valid identifier')
else:
    print(str2, 'is not a valid identifier')

Output

                    

Hello_World_100 is a valid identifier
101 Python Programming Hacks is not a valid identifier

What if the string is a Python Keyword?

We have learnt that we should avoid using Python keywords as identifiers. But surprisingly, the isidentifier() function returns True for a string containing a Python keyword, even though it is not recognized as a valid identifier.

Example

                    

s = 'class'
print(s.isidentifier())

Output

To resolve this issue and to test whether a string input matches with the Python keywords, we use the iskeyword() function. To implement this function, we need to import this built-in function from the keyword module.

Example

                    

from keyword import iskeyword
print(iskeyword('class'))

Output

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.