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 Convert Decimal to Binary, Octal and Hexadecimal
Python while Loop
Python exec()
Python Program to Find Armstrong Number in an Interval
Python String casefold()
Python Inheritance
Python Program to Check Leap Year
Python callable()
Python String translate()
How to Remove the Last Character of a String?
Python Data Structures and Algorithms - Benjamin Baka
Jackson – Marshall String to JsonNode
Python isinstance()
Python bool()
Python List sort()
Python complex()
Python len()
Count Occurrences of a Char in a String
Python if...else Statement
Python Program to Trim Whitespace From a String
Python bytearray()
Python Multiple Inheritance
Python classmethod()
Python List extend()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Set discard()
Machine Learning with Python for everyone - Mark E.Fenner
Python Set pop()
Python String partition()
Python Program to Calculate the Area of a Triangle
Python Program to Concatenate Two Lists
Python Dictionary setdefault()