Table of Contents
The isidentifier() method returns True if the string is a valid identifier in Python. If not, it returns False.
The syntax of isidentifier() is:
string.isidentifier()
1. isidentifier() Paramters
The isidentifier() method doesn’t take any parameters.
2. Return Value from isidentifier()
The isidentifier() method returns:
- True if the string is a valid identifier
- False if the string is not a invalid identifier
3. Example 1: How isidentifier() works?
str = 'Python' print(str.isidentifier()) str = 'Py thon' print(str.isidentifier()) str = '22Python' print(str.isidentifier()) str = '' print(str.isidentifier())
Output
True False False False
Visit this page to learn what is valid identifier in Python?
4. Example 2: More Example of isidentifier()
str = 'root33' if str.isidentifier() == True: print(str, 'is a valid identifier.') else: print(str, 'is not a valid identifier.') str = '33root' if str.isidentifier() == True: print(str, 'is a valid identifier.') else: print(str, 'is not a valid identifier.') str = 'root 33' if str.isidentifier() == True: print(str, 'is a valid identifier.') else: print(str, 'is not a valid identifier.')
Output
root33 is a valid identifier. 33root is not a valid identifier. root 33 is not a valid identifier.
Related posts:
Python Dictionary
Python Program to Represent enum
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python RegEx
Python Program to Find LCM
How to Get Started With Python?
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Program to Randomly Select an Element From the List
Python Program to Find the Sum of Natural Numbers
Python globals()
Python len()
Python Program to Solve Quadratic Equation
Python String title()
Python Tuple
Python Program to Return Multiple Values From a Function
Python Set add()
Python del Statement
Case-Insensitive String Matching in Java
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python oct()
Python Anonymous / Lambda Function
Python List insert()
Python *args and **kwargs
CharSequence vs. String in Java
Python String lstrip()
Python String islower()
Python zip()
Python Program to Iterate Through Two Lists in Parallel
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Program to Differentiate Between type() and isinstance()
Python complex()
Python Program to Swap Two Variables