isidentifier() method of str class

isidentifier() method of str class in checks whether the contents of a string is a valid identifier.Returns true if its is a valid identifier, else returns False.

Str Updated 11 Mar 2017 1 min read

Method Name

isidentifier

Method Signature

isidentifier ()

Method Overview

  • isidentifier() method checks whether the contents of the string object forms a valid identifier as per Python syntax rules.

  • Returns, True if the contents of the string qualifies as a Python identifier. False otherwise.

Example

python
# starts with digit

id1     = "1_name"

idCheck = id1.isidentifier()

print(idCheck)

# starts with digit and contains special characters

id2     = "1_name#"

idCheck = id2.isidentifier()

print(idCheck)

# starts with alphabet and contains special characters

id3     = "name#"

idCheck = id3.isidentifier()

print(idCheck)

# starts with alphabet, contains underscore and digit

id4     = "name_1"

idCheck = id4.isidentifier()

print(idCheck)

Output

Output
False

False

False

True