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 = "w3wiki" 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 = "w3wiki" 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. w3wiki is an identifier. admin#1234 is not an identifier. user@12345 is not an identifier.
Python String isidentifier() Method
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.