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 Check if a Key is Already Present in a Dictionary
Python repr()
Python String lower()
Python Dictionary keys()
Python Program to Make a Simple Calculator
Python Modules
Python Set pop()
Python set()
Check if a String is a Palindrome in Java
Python String rstrip()
Python String encode()
Python Program to Compute the Power of a Number
Python Program to Find HCF or GCD
Python eval()
Python object()
Python Program to Capitalize the First Character of a String
Python String capitalize()
Python len()
Python Program to Count the Number of Occurrence of a Character in String
Python String format()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python slice()
Python File I/O Operation
Python oct()
Python Set add()
Python String rjust()
Python Deep Learning Cookbook - Indra den Bakker
String Processing with Apache Commons Lang 3
Python List insert()
Python Program to Check If Two Strings are Anagram
Python Type Conversion and Type Casting
Python Program to Iterate Over Dictionaries Using for Loop