Introduction: Python String isidentifier() Method
When writing Python programs, you often need to ensure that variable, function, or class names follow Python’s naming rules.
The Python String isidentifier() method provides a quick way to check whether a string is a valid Python identifier.
What it is:
It is a built-in Python string method that returns True if a string follows the rules for identifiers and False otherwise.
This method is commonly used for validating dynamically generated variable names, checking user input, or filtering lists of strings before using them as identifiers. This is especially useful in code editors, linters, or automated scripts that generate Python code.
Let’s explore the isidentifier() method—what it does, why it’s useful, and how to use it in Python programs.
Continue With String Methods: There are many more useful string methods beyond this one. Explore the full Python String Methods List
Python String isidentifier() Method: Syntax, Parameter, Return Value and Examples
This section covers everything you need to know about the isidentifier() method, including its syntax, parameters, return values and practical examples in Python programs.
Syntax
Here is the basic syntax of the isidentifier() method
string.isidentifier()
- The method is called on a string object.
- No parameters are required.
- The result is either
TrueorFalse.
Return Value
It returns True if the string follows Python identifier rules otherwise False.
- True → The string is a valid Python identifier.
- False → The string breaks one or more identifier rules, such as starting with a number or containing spaces.
Quick Example
name1 = "my_var"
print(name1.isidentifier()) # True
name2 = "123abc"
print(name2.isidentifier()) # False
The first string is valid because it follows Python’s naming rules. The second string starts with a digit, so it is invalid.
Examples of the Python String isidentifier() Method
To see how the Python isidentifier() Method behaves in real situations, it helps to test it with different types of strings.
The examples below illustrate common identifier scenarios and how Python evaluates them.
Example 1: Checking Valid Identifiers
Let’s test strings with different patterns to see which are valid identifiers.
The following examples test different strings to see whether they qualify as valid identifiers in Python.
#Example 1.1: Simple Valid Identifier
print("variable".isidentifier()) # True
#The string "variable" starts with a letter and contains only alphabets, making it a valid identifier.
#Example 1.2: Identifier Starting with a Digit (Invalid)
print("2number".isidentifier()) # False
#An identifier cannot begin with a digit, which makes "2number" invalid.
#Example 1.3: Identifier Containing Digits
print( "var123".isidentifier()) # True
#Digits are allowed after the first character, so "var123" is a valid identifier.
#Example 1.4: Identifier Containing Spaces (Invalid)
print("user name".isidentifier()) # False
#Python identifiers cannot contain spaces. The string "user name" is therefore not a valid identifier.
#Example 1.5: Identifier with Special Characters (Invalid)
print("data@123".isidentifier()) # False
#Special characters like $, @, or # are not permitted in Python identifiers. Hence, "data@123" is invalid.
#Example 1.6: Unicode Characters in Identifiers
name = "变量" # Chinese characters
print(name.isidentifier()) # Output: True
#Python supports Unicode identifiers, meaning variable names can include non-English characters like Chinese, Greek, or Hindi letters.
#Example 1.7: Empty String (Invalid)
name = ""
print(name.isidentifier()) # Output: False
#An empty string cannot serve as an identifier because it contains no characters to define a valid name.
#Example 1.8: Identifier Starting with an Underscore
print("_temp".isidentifier()) # True
#Identifiers can begin with an underscore (_), which is commonly used for private or internal variables in Python.
Explanation:
Each example demonstrates how Python evaluates identifier rules. Names such as variable, var123, and _temp return True because they follow the allowed naming rules.
Strings containing spaces, special characters, or starting with digits return False. The method also confirms that Python supports Unicode identifiers, which means variable names can include characters from many languages.
Example 2: Checking Reserved Words
Next, check how isidentifier() handles Python reserved words.
The isidentifier() method only checks if a string follows Python’s naming rules. It does not check if the string is a reserved keyword.
print("for".isidentifier()) # True
print("class".isidentifier()) # True
# Output:
# True
# True
Explanation:
Both for and class follow the rules for valid identifiers (letters only, no spaces, etc.), so isidentifier() returns True.
However, these are Python keywords, so they cannot be used as variable names in actual programs.
Example 3: Real-World Use Case — Validating User Input
Here’s a practical scenario validating user input.
You can use isidentifier() to validate names entered by users or generated dynamically before using them in code.
var_name = input("Enter a variable name: ")
if var_name.isidentifier():
print("Valid variable name.")
else:
print("Invalid variable name.")
# Output if user enters 'my_variable':
# Valid variable name.
Explanation:
The program checks if the entered name follows Python identifier rules.
This prevents syntax errors and ensures only valid names are used.
Example 4: Python Keyword (Syntactically Valid but Reserved)
Finally, see how reserved keywords behave, even if syntactically valid.
Sometimes a string may pass isidentifier() but still be a reserved keyword. In such cases, use the keyword module for full validation.
name = "for"
print(name.isidentifier()) # True
import keyword
print(keyword.iskeyword("for")) # True
Explanation:
isidentifier() returns True because for follows naming rules.
But keyword.iskeyword() shows it’s reserved, so it cannot be used as a variable name.
Common Use Cases: When to Use the Python String isidentifier() Method
The Python isidentifier() Method becomes useful whenever a program needs to verify identifier names before using them in code.
- Validating dynamically generated variable names.
- Building code editors, linters, or syntax checking tools.
- Filtering valid identifiers from a list of strings.
- Ensuring safe identifiers in automated code generation.
Key Takeaways: Python String isidentifier() Method
The Python isidentifier() method provides a simple way to confirm whether a string follows Python’s identifier naming rules.
- Checks whether a string is syntactically a valid identifier.
- Returns a Boolean value (
TrueorFalse). - Does not check whether the name is a Python keyword.
- Works well alongside
keyword.iskeyword()for full validation.