The isidentifier() function in python helps to check whether the specified string is a valid identifier or not. The function returns true if it is a valid identifier otherwise it returns false.
string.isidentifier()
The isidentifier() method doesn't take any parameters. A string is said to be a valid identifier if it contains alphanumeric letters (a-z) and numbers(0-9), or underscores (_), and can be of any length. Also, the valid identifier cannot start with a number, or contain any spaces.
The return value is always a boolean value.In the case of an empty string, the function will return False.
Input | Return Value |
---|---|
if a valid identifier | True |
if not a valid identifier | False |
string = 'Programming'
print(string.isidentifier())
string = 'Pro gramming'
print(string.isidentifier())
string = '12Programming'
print(string.isidentifier())
string = ''
print(string.isidentifier())
Output:
True False False False
string1 = "Python"
string2 = "Python002"
string3 = "1python"
string4 = "hi python"
print(string1.isidentifier())
print(string2.isidentifier())
print(string3.isidentifier())
print(string4.isidentifier())
Output:
True True False False