Python - String Method - isidentifier() Tutorial
The isidentifier() method returns True if all characters in the string are valid identifiers. otherwise, it returns False.
Syntax-
String.isidentifier()
Valid and Invalid identifiers:
Valid Identifier |
Invalid Identifier |
Comment |
false |
False |
“False” is Boolean value or Keyword,1st letter of Boolean value should be capital. While “false” is not keyword and can be used as a valid identifier |
Else |
else |
Else is not a keyword as it is capitalize. While “else” is keyword and cannot be used as a identifier. |
_sam1 , sam1_ |
sam+s , Sam. , vai@ |
Only underscore (special character ) is a valid identifier. |
sam_3 |
1sam_ , 33 |
Valid identifier cannot start with digit and can’t be only digit |
|
for , finally , is |
Reserved Keyword cannot be valid identifier |
Example-
str1 = "sam 3"
print(str1.isidentifier())
str2 = "@sam"
print(str2.isidentifier())
str3 = "_sam12"
print(str3.isidentifier())
str4 = "3sam"
print(str4.isidentifier())
str5 = "sam+s"
print(str5.isidentifier())
Output-
False
False
True
False
False