Skip to content
Related Articles

Related Articles

Python String isidentifier() Method

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 09 Sep, 2022
Improve Article
Save Article

Python String isidentifier() method is used to check whether a string is a valid identifier or not. The method returns True if the string is a valid identifier, else returns False.

Python String isidentifier() method Syntax

Syntax:  string.isidentifier()

Parameters: The method does not take any parameters

Return Value:  The method can return one of the two values: 

  • True: When the string is a valid identifier.
  • False: When the string is not a valid identifier.

Python String isidentifier() Method Example

Python3




string = "Coding_101"
print(string.isidentifier())

Output:

True

Note: A string is considered as a valid identifier if:

  • It only consists of alphanumeric characters and underscore (_)
  • Doesn’t start with a space or a number

Example 1: How isidentifier() works

Here, we have added some basic examples of using isidentifier() Method in Python

Python3




# String with spaces
string = "Geeks for Geeks"
print(string.isidentifier())
 
# A Perfect identifier
string = "GeeksforGeeks"
print(string.isidentifier())
 
# Empty string
string = ""
print(string.isidentifier())
 
# Alphanumerical string
string = "Geeks0for0Geeks"
print(string.isidentifier())
 
# Beginning with an integer
string = "54Geeks0for0Geeks"
print(string.isidentifier())

Output: 

False
True
False
True
False

Example 2: Using Python String isidentifier() Method in condition checking

Since, Python String isidentifier() returns boolean we can directly use isidentifier() inside if-condition to check if a String is an indicator or not.

Python3




string = "geeks101"
if string.isidentifier():
    print(string, "is an identifier.")
else:
    print(string, "is not an identifier.")
 
string = "GeeksForGeeks"
if string.isidentifier():
    print(string, "is an identifier.")
else:
    print(string, "is not an identifier.")
 
string = "admin#1234"
if string.isidentifier():
    print(string, "is an identifier.")
else:
    print(string, "is not an identifier.")
 
string = "user@12345"
if string.isidentifier():
    print(string, "is an identifier.")
else:
    print(string, "is not an identifier.")

Output:

geeks101 is an identifier.
GeeksForGeeks is an identifier.
admin#1234 is not an identifier.
user@12345 is not an identifier.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!