.isidentifier()
The .isidentifier()
string method takes in a string and returns True
if the string is a valid Python identifier, else returns False
.
Syntax
string.isidentifier()
- Python
identifiers
must start with a letter (a-z, A-Z
) or an underscore (_
) followed by letters, digits (0-9
), or underscores. - They cannot be a Python keyword like
if
,else
,while
, etc.
Example
In the code below .isidentifier()
to check if a string is a valid Python identifier:
my_string = "Codecademy"print(my_string.isidentifier());
This example results in the following output:
True
Codebyte Example
The following code is runnable and uses .isidentifier()
to check if my_string
is a valid Python identifier:
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.