ididentifier() Method



Description

The isidentifier() method checks whether the string is a valid Python identifier. In Python, the identifier should start with an alphabet or underscore, and may contain one or more alphabets (A-Z, a-z), digits or underscore.

Syntax

var.isidentifier()

Parameters

NA

Return Value

This method returns true if the string is a valid identifier. It returns false otherwise.

Example

The following example shows the usage of isidentifier() method.

var = "Hello Python"
var1 = var.isidentifier()
print ("original string:", var)
print ("is a valid identifier?:", var1)

var = "\u0041\u0042\u0043"
var2 = var.isidentifier()
print ("original string:", var)
print ("is a valid identifier?:", var2)

var = "total_marks"
var3 = var.isidentifier()
print ("original string:", var)
print ("is a valid identifier?:", var3)

When you run this program, it will produce the following output

original string: Hello Python
is a valid identifier?: False
original string: ABC
is a valid identifier?: True
original string: total_marks
is a valid identifier?: True

Here, 0041 is Unicode for A, 0042 is Unicode for B 0043 is Unicode for C

boolean_string_methods.htm
Advertisements