1. What is the isidentifier() Function in Python?
The isidentifier() method is a built-in string function in Python used to check whether a given string is a valid Python identifier. In simpler words, it determines if a string can safely be used as a variable name, function name, class name, or any identifier without raising a syntax error.
In Python, an identifier is considered valid if it:
If every character in the string qualifies as a digit and the string is not empty, this method returns True; otherwise, it returns False.
- Starts with a letter (A–Z or a–z) or an underscore (_).
- Can include letters, digits (0–9), or underscores after the first character.
- Does not start with a number or contain spaces or special symbols (like @, $, %, etc.).
2. Purpose of the isidentifier() Function
TThe isidentifier() method is used to check whether a string is a valid Python identifier. In simple terms, it tells you whether a word or name can be legally used as a variable name, function name, class name, or any other identifier in Python.
Python identifiers must follow specific rules:
- They can only contain letters (A–Z, a–z), digits (0–9), and underscores (_).
- They cannot start with a digit.
- They cannot contain spaces or special characters.
- They cannot be Python keywords (e.g., for, if, class), though
3. Syntax of isidentifier() Function
string.isidentifier()
Explanation:
- The method is called on a string object
- It does not take any parameters.
- The result is either True or False based on identifier validity.
4. Parameter Description: Python isidentifier() Method
| Parameter | Type | Description |
|---|---|---|
| None | – | The isidentifier() method does not accept any parameters. |
Return Value
The method returns a Boolean value (True or False) based on the content of the string.
- Returns True → if the string is a valid Python identifier.
- False – if the string contains any non-digit characters (letters, punctuation, spaces, symbols, or is empty).
5. Examples and Explanations of the Python isidentifier() Method
Below are several examples showing how the isidentifier() method works with different types of strings. Each example demonstrates whether a given string qualifies as a valid Python identifier and why.
Example 1: Checking Valid Identifiers
print("variable".isidentifier()) # True
#The string "variable" starts with a letter and contains only alphabets, making it a valid identifier.
print("2number".isidentifier()) # False
#An identifier cannot begin with a digit, which makes "123var" invalid.
print( "var123".isidentifier()) # True
#Digits are allowed after the first character, so "var123" is a valid identifier.
print("_temp".isidentifier()) # True
#Identifiers can begin with an underscore (_), which is often used to indicate private variables in Python.
print("user name".isidentifier()) # False
#Python identifiers cannot contain spaces. The string "my var" is therefore not a valid identifier.
print("data@123".isidentifier()) # False
#Special characters like $, @, or # are not permitted in Python identifiers. Hence, "var$123" is invalid.
Explanation:
“variable” → Valid identifier (starts with a letter).
“2number” → Invalid (cannot start with a number).
“var123” → Valid identifier (starts with a letter).
“_temp” → Valid (starts with underscore).
“user name” → Invalid (contains space).
“data@123” → Invalid (contains special character @).
Example 2: Empty String (Invalid)
name = ""
print(name.isidentifier()) # Output: False
Explanation: An empty string cannot serve as an identifier because it contains no characters to define a valid name.
Example 3: Real-World Use Case — Validating User Input
Suppose you’re designing a Python IDE or code editor that checks if the entered variable name is valid:
var_name = input("Enter a variable name: ")
if var_name.isidentifier():
print("✅ Valid variable name.")
else:
print("❌ Invalid variable name.")
#Output:
Enter a variable name: my_variable
Valid variable name.
Explanation: This approach helps prevent syntax errors before execution by validating variable names early.
Example 4: Python Keyword (Syntactically Valid but Reserved)
name = "for"
print(name.isidentifier()) # Output: True
import keyword
print(keyword.iskeyword("for")) # Output: True
Explanation: The word “for” is syntactically valid according to identifier rules, so isidentifier() returns True. However, it’s also a Python keyword, meaning it cannot be used as a variable name. To verify this, the keyword module can be used as shown above.
6. When to Use isidentifier()
Use the isidentifier() function when you want to:
- Validate dynamically generated variable names or identifiers.
- Build tools such as code linters, IDEs, or syntax checkers.
- Filter invalid variable names from a list of strings.
- Ensure safe use of identifiers in metaprogramming or code generation tasks.
7. Key Takeaways
- isidentifier() checks syntax-level validity of strings as identifiers.
- Returns True/False — no parameters needed.
- Does not check for Python reserved words (combine with keyword.iskeyword() for that).
- Useful for code validation, automation, and safety checks.
8. Python isidentifier() — Summary Table
Python isidentifier() — Quick Summary
A compact reference table showing common examples and the boolean result of str.isidentifier().
| Example | Description | Result |
|---|---|---|
"name".isidentifier() |
Valid identifier | True |
"2name".isidentifier() |
Starts with a digit | False |
"_user".isidentifier() |
Starts with underscore | True |
"user name".isidentifier() |
Contains a space | False |
"class".isidentifier() |
Keyword, but syntactically a valid identifier | True |
Tip: isidentifier() checks syntax only — to test whether a name is a reserved keyword use import keyword; keyword.iskeyword(name).