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 Set symmetric_difference_update()
Python Program to Find LCM
APIs in Node.js vs Python - A Comparison
Python List Comprehension
Python Tuple
Python reversed()
Python List reverse()
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Program to Check Armstrong Number
Python String center()
Python String endswith()
Python List count()
Python Program to Shuffle Deck of Cards
Python isinstance()
Python for Loop
Python String index()
Python String replace()
Python Program to Get the Class Name of an Instance
Python str()
Python Program to Delete an Element From a Dictionary
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Check If a String Is Numeric in Java
Python pow()
Python Program to Get the Full Path of the Current Working Directory
Python Program to Append to a File
Python Program to Split a List Into Evenly Sized Chunks
Python Program to Safely Create a Nested Directory
Python String find()
Python Set copy()
CharSequence vs. String in Java
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Deep Learning with Python - Francois Cholletf