Python .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
identifiersmust 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
- Learn to analyze and visualize data using Python and statistics.
- Includes 8 Courses
- With Certificate
- Intermediate.13 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours