Python String isidentifier() Method

By Lenin Mishra

If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.

Python String isidentifier() method

The isidentifier() method in Python returns True, if the string is a valid identifier.

What is a valid identifier in Python?
  1. Should contain alphanumeric characters - (a-z) and (0-9).
  2. Can have underscores (_).
  3. Shouldn’t contain spaces.
  4. Cannot start with a number.
Syntax of isidentifier() method
string.isidentifier()

Returns True or False
Example 1

Code/Output

# only alphabets
str1 = "Pylenin"
print(str1.isidentifier())
>>> True

# Only numbers
str2 = "1992"
print(str2.isidentifier())
>>> False

# alphanumeric characters
str3 = "Pylenin1992"
print(str3.isidentifier())
>>> True

# alphanumeric characters starting with numbers
str4 = "1992Pylenin"
print(str4.isidentifier())

# special characters like '@'
str5 = "pylenin1992@gmail.com"
print(str5.isidentifier())
>>> False

# sentences
str6 = "Pylenin loves Python"
print(str6.isidentifier())
>>> False

As you can see, the isidentifier() method in Python returns False even for numerical strings.

Example 2 - Working with underscores

Code/Output

# underscores
str1 = "_"
print(str1.isidentifier())
>>> True

# alphabets with underscores
str2 = "Pylenin_loves_Python"
print(str2.isidentifier())
>>> True

# numbers with underscores
str3 = "1234_5678_90"
print(str3.isidentifier())
>>> False

# alphanumeric with underscores
str4 = "Pylenin_1992"
print(str4.isidentifier())
>>> True

Check out other commonly used Python string methods.

  1. How to create a string in Python?
  2. How to access characters in a Python string?
  3. How to replace characters in a string in Python?
  4. How to concatenate strings in Python?
  5. How to iterate through a string in Python?
  6. Check if a Substring is Present in a Given String in Python
  7. Escape sequences in Python String
  8. Python String Formatting - The Definitive Guide