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()
Call
What 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 Value
Description
bool
True 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())
Output
name True
_hidden True
2fast False
my-var False
has space False
How It Works
Plain words pass.name and _hidden use only allowed characters and do not start with a digit.
A leading digit fails.2fast starts with a digit, so it cannot be a name.
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)
Output
for True False
total True True
None True False
How It Works
Shape only. All three words have the shape of a name, so isidentifier() prints True for each.
Reserved words are caught.keyword.iskeyword() is True for for and None, so the combined test is False.
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))
Output
['first_name', 'age_years', 'city']
True
How It Works
Lower and replace.lower() and replace() remove capitals and turn spaces into underscores.
Brackets are dropped. The two extra replace() calls delete the parentheses, which are not allowed in a name.
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.