How to Use String isidentifier() Method in PythonPython Basics


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

Note: A valid identifier only contains alphanumeric letters (a-z) and (0-9), or underscores (_). In order to be valid, it can not start with a number, or contain any spaces.

Example

str = 'ABCCBA'
print(str.isidentifier())

# not valid due to space in the string
str = 'ABC CBA'
print(str.isidentifier())

# not valid due to # in the string
str = 'A#BC'
print(str.isidentifier())

# not valid due to starting with a number
str = '9ABC'
print(str.isidentifier())

Output:

True
False
False
False

Syntax

string.isidentifier()

Parameters

The method doesn’t take any parameters.

Return Value

It returns False and True.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us