Join Regular Classroom : Visit ClassroomTech

isidentifier() method in Python String

It is a method in python that checks if the given string is a valid identifier or not. It returns True if the condition is satisfied else False.

A valid identifier-

  1. Cannot start with a number.

  2. Contains only alphanumeric characters (A-Z, a-z ,0-9) or underscores(_).

  3. Cannot contain any spaces or special characters.

Syntax:

string.isidentifier()

Example 1:

s = “Code
x = s.isidentifier()
print(x)

Output:

True

Explanation:

Here it contains only alphabets which satisfies the above 3 conditions. Hence returned True.

Example 2:

s = “_Code123
x = s.isidentifier()
print(x)

Output:

True

Explanation:

Here it contains only alphabets and numbers with a_” in the beginning which satisfies the above 3 conditions. Hence returned True.

Example 3:

s = “1Code123
x = s.isidentifier()
print(x)

Output:

False

Explanation:

Here the string starts with a number, which is against the condition. Hence returned False.

You Missed
Also Checkout