The isidentifier()
method is a part of string methods in Python and is used to check if a given string is a valid identifier. An identifier is a sequence of alphanumeric characters (a-z, A-Z, 0-9) and underscores (_) that does not start with a number. The method returns True
if the string is a valid identifier, and False
otherwise.
Parameter Values
This function does not accept any parameters.Return Values
The isidentifier()
method returns a bool
, either True
or False
.
How to Use isidentifier()
in Python
Example 1:
Checks if a string is a valid Python identifier.
name = 'my_variable'
print(name.isidentifier())
Example 2:
Returns False if the string is not a valid identifier.
string = '123_var'
print(string.isidentifier())
Example 3:
Returns True if the string is a valid identifier.
text = 'valid_identifier_123'
print(text.isidentifier())