Python String isidentifier() Method



The Python String isidentifier() method is used to determine if a given string is a valid identifier or not. An identifier is a name the user declares to variables, classes or any other attribute.

In Python, an identifier is said be valid if it is a non-empty string that only contains alphanumeric letters (a-z, A-Z), digits (0-9), or underscores (_). Additionally, it should not begin with a digit.

Syntax

Following is the syntax of the Python String isidentifier() method −

string.isidentifier()

Parameters

The Python String isidentifier() method does not accept any parameters.

Return Value

The Python String isidentifier() method returns a Boolean value (either True or Flase).

Example

If the given identifier follows the rules of declaring of Python identifiers, then the isidentifier() method will return True. In the following example, all the declared variables are valid, hence, the code will return True for all cases.

print("List of valid identifiers:")
strOne = "TutorialsPoint"
strTwo = "_TutorialsPoint"
strThree = "TutorialsPoint01"
print(strOne.isidentifier())  
print(strTwo.isidentifier())  
print(strThree.isidentifier())

When we run above program, it produces following result −

List of valid identifiers:
True
True
True
python_string_methods.htm
Advertisements