.isidentifier()
Published Sep 23, 2023
Contribute to Docs
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:
Contribute to Docs
- 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.
Learn Python on Codecademy
- Skill path
Analyze Data with Python
Learn to analyze and visualize data using Python and statistics.Includes 8 CoursesWith CertificateIntermediate13 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours