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 Extract Extension From the File Name
Python String find()
Python String replace()
Adding a Newline Character to a String in Java
Python Tuple count()
Converting a List to String in Java
Python Program to Check the File Size
Python Program to Check if a Key is Already Present in a Dictionary
Python del Statement
Python Shallow Copy and Deep Copy
Python oct()
Python super()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python String center()
String Hashing
Python setattr()
Python Program to Sort a Dictionary by Value
Python Program to Parse a String to a Float or Int
Python id()
Python Program to Access Index of a List Using for Loop
Python while Loop
Python Program to Get the File Name From the File Path
Python Program to Compute all the Permutation of the String
Converting String to Stream of chars
Why String is Immutable in Java?
Python Program to Find the Factorial of a Number
Python Program to Remove Duplicate Element From a List
Python Dictionary update()
Python List reverse()
Python Program to Check Prime Number
Python Directory and Files Management
Python RegEx