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 String swapcase()
Python Sets
JavaScript Eval: run a code string
Python str()
Python min()
Python Data Types
Most commonly used String methods in Java
Python strftime()
Convert char to String in Java
String Set Queries
Deep Learning in Python - LazyProgrammer
Python Set issuperset()
Python Program to Check If Two Strings are Anagram
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Functions
Python Function Arguments
Python List Comprehension
Python String strip()
Python Program to Count the Number of Occurrence of a Character in String
Python id()
Count Occurrences of a Char in a String
Python Errors and Built-in Exceptions
Python Program to Check If a String Is a Number (Float)
Python abs()
Python Set union()
Python Program to Find Factorial of Number Using Recursion
Python Recursion
Python String find()
Python List
Python ord()
Python List clear()
Python staticmethod()