.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:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Python on Codecademy