Python isidentifier() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

isidentifier() tells you whether a string could be used as a name in Python: a variable, function, or class name. A valid name is made of letters, digits, and underscores, and it must not start with a digit.

The check follows the language rules, but it does not know about reserved words. "for" and "None" look like names, so they return True even though you cannot use them as variables. Add keyword.iskeyword() to reject those. An empty string is not an identifier.

text.isidentifier() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.

Syntax

Call it on any string. There are no arguments, but the parentheses are required.

python
ok = text.isidentifier()
CallWhat it does
"total_2".isidentifier()Returns True. Letters, digits, and underscores are allowed, as long as the first character is not a digit.
"2total".isidentifier()Returns False. A name cannot start with a digit.
"for".isidentifier()Returns True. It has the right shape, but for is a reserved word that you cannot use as a variable name.

To test for reserved words import keyword and call keyword.iskeyword(text). To test only letters and digits use isalnum().

Library declarations
python
str.isidentifier() -> bool
# returns: True if the string is a valid identifier (keywords included)
# an empty string returns False

Parameters

This method takes no arguments. It always checks the whole string.

Return Value

Return ValueDescription
boolTrue if the string is a valid identifier according to the language rules, otherwise False.

Python Examples

Example 1 — Which Words Are Valid Names?

Test five words against the naming rules.

python
for word in ["name", "_hidden", "2fast", "my-var", "has space"]:
    print(word, word.isidentifier())

How It Works

  1. Plain words pass. name and _hidden use only allowed characters and do not start with a digit.
  2. A leading digit fails. 2fast starts with a digit, so it cannot be a name.
  3. A hyphen or a space fails. my-var and has space contain characters that are not allowed in a name.

Example 2 — Add a Keyword Check

Compare isidentifier() alone with the combined test that also rejects reserved words.

python
import keyword

for word in ["for", "total", "None"]:
    usable = word.isidentifier() and not keyword.iskeyword(word)
    print(word, word.isidentifier(), usable)

How It Works

  1. Shape only. All three words have the shape of a name, so isidentifier() prints True for each.
  2. Reserved words are caught. keyword.iskeyword() is True for for and None, so the combined test is False.
  3. A normal name passes both. total is valid and is not reserved, so the last column is True.

Example 3 — Turn Headings Into Variable Names

Clean spreadsheet headings, then confirm that every result is a valid name.

python
headers = ["First Name", "Age (years)", "city"]
clean = []
for heading in headers:
    name = heading.lower().replace(" ", "_")
    clean.append(name.replace("(", "").replace(")", ""))

print(clean)
print(all(name.isidentifier() for name in clean))

How It Works

  1. Lower and replace. lower() and replace() remove capitals and turn spaces into underscores.
  2. Brackets are dropped. The two extra replace() calls delete the parentheses, which are not allowed in a name.
  3. Verify the result. all() runs isidentifier() on every cleaned heading and returns True only if each one passes.

Frequently Asked Questions

It returns True if the string is a valid Python identifier: it contains only letters, digits, and underscores and does not start with a digit.
No. They have the shape of a name, so it returns True. Use keyword.iskeyword(text) to detect them.
No. A space or a hyphen makes isidentifier() return False. Replace them with underscores to get a valid name.
False. An identifier needs at least one character.
isalnum() accepts any letters and digits, including a leading digit, and rejects underscores. isidentifier() allows underscores but not a leading digit.

Next: islower()

True if all letters are lower case and there is at least one letter.

islower() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful