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 Program to Swap Two Variables
Python super()
Python Input, Output and Import
Python Program to Multiply Two Matrices
Python bool()
Python Dictionary get()
Python String split()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
String Set Queries
Python Matrices and NumPy Arrays
Python Program to Print Hello world!
Python Program to Find the Factorial of a Number
Python Set issuperset()
Python Program to Delete an Element From a Dictionary
Python String isdecimal()
Python help()
Python Program to Find the Factors of a Number
Python String format_map()
Python List copy()
Python Program Read a File Line by Line Into a List
Python String index()
Python Program to Find Factorial of Number Using Recursion
Python Dictionary pop()
Python zip()
Python Numbers, Type Conversion and Mathematics
Python Program to Find the Size (Resolution) of a Image
Python sum()
Python Program to Transpose a Matrix
Python List reverse()
Check If a String Is Numeric in Java
Python Program to Catch Multiple Exceptions in One Line
Python Variables, Constants and Literals