How to check if a string is a valid identifier or not in Python

In this tutorial, we will learn how to check if the given string is a valid identifier or not in Python with some easy examples.

We can check for the same in many ways, some of which would be by using regular expressions or the isidentifier() built-in function.

What is a valid identifier in Python

Let’s first take a look at a few examples-

Examples:-

  1. Valid: _abc, Abc1, aBc2_d, a234_bC
  2. Invalid: 2abc, ab cd, 3444, #gh, ab&cd

Points to remember-

  • You cannot use reserved keywords as an identifier name.
  • Identifiers can contain all the letters (Small, upper both), all the digits from 0 to 9, and underscore.
  • The identifier name cannot begin with a digit.
  • Python identifier names are case sensitive and they do not have any limit on the length of the identifier name.

How to check if a string is a valid identifier or not in Python

Now, the question is how we can check if the string provided by the user is a valid identifier or not.

We would be using two methods –

  1. Regular Expressions
  2. isidentifier() built-in function

Using Regular Expressions

A regular expression is simply a sequence of characters. We create a regular expression so that a pattern is created which can be used for matching or searching in strings in python. Also, the use of several metacharacters makes it easy to work with strings in python. And we make use of regular expressions by importing re module.

To learn more about regular expressions and metacharacters visit-

First, we need to import the re module. After that, we create a variable that can take the user_input(string). Then we make a pattern that consists of a sequence of characters necessary for a valid identifier. And we use this pattern to search whether the string is a valid identifier or not.

import re
s = input("Enter a string")
pattern = r'^[a-zA-Z_]\w*$'
if(re.search(pattern,s)):
    print("\n Valid")
else:
    print("\n Invalid")

In the above code, the pattern is ‘^[a-zA-Z_]\w*$’, which contains-

  • ^[a-zA-Z_] means that the string should start with either lower case characters, upper case characters or underscore.
  • \w is used for word matching character which can be lower case, upper case, digit or underscore.
  • * is used for any number of specified characters.
  • $ is used to specify that the string ends with the mentioned character/characters.
  • The search() method returns a search object only when the pattern match is found in the string. Therefore, the ‘if’ statement will only get executed when an object is returned otherwise ‘else’ will get executed.

When we run the above code for-

Input 1:

_abc

Output 1:

Valid

Input 2:

#abc

Output 2:

Invalid



Using isidentifier() built-in function

The isidentifier() is a built-in function which returns true when the given string is a valid identifier else it returns false.

First, we have created a variable that can take the user_input(string). And then have used the dot operator with the isidentifier() function which will return either true or false. This means that ‘if’ statement will only get executed if ‘true’ value is returned.

s = input("Enter a string")
if(s.isidentifier()):
    print("\n Valid")
else:
    print("\n Invalid")

When we run the above code for-

Input 1:

ab_4def

Output 1:

Valid

Input 2:

abc def

Output 2:

Invalid

Leave a Reply

Your email address will not be published. Required fields are marked *