Python String isidentifier()

The Python String isidentifier() method is a built-in function that returns true if the string is a valid identifier. If not it returns False.

In this article, we will learn about the Python String isidentifier() method with the help of examples.

isidentifier() Syntax

The Syntax of isidentifier() method is:

string.isidentifier()

isidentifier() Parameters

The isidentifier() method does not take any parameters.

isidentifier() Return Value

The isidentifier() method returns

  • True if string is a valid identifier
  • False if the string is not a valid identifier

Checkout to documentation to learn more about What is a valid identifier in Python?

Example 1: How isidentifier() works?


# Python code to illustrate 
# the working of isidentifier()
  
# String with spaces
string = "Its My Code"
print(string.isidentifier())
  
# A Valid identifier
string = "ItsMyCode"
print(string.isidentifier())
  
# Empty string
string = ""
print(string.isidentifier())

# Underscore string
string = "_"
print(string.isidentifier())
  

# Alphanumeric string
string = "ItsMyC0de123"
print(string.isidentifier())
  
# Beginning with an integer
string = "123ItsMyCode"
print(string.isidentifier())

Output

False
True
False
True
True
False

Example 1: How to use isidentifier() method?

# Python isidentifier() method example

str = "ItsMyCode"
# check if the string is valid identifier
if str.isidentifier() == True:
    print("{text} is an identifier".format(text=str))
else:
    print("{text} is not a valid identifier".format(text=str))
str2 = "$$ItsMyCode$$"
if str2.isidentifier() == True:
    print("{text} is an identifier".format(text=str2))
else:
   print("{text} is not a valid identifier".format(text=str2))

Output

ItsMyCode is an identifier
$$ItsMyCode$$ is not a valid identifier
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python String Rsplit()

Python String rsplit()

Table of Contents Hide rsplit() Syntaxrsplit() Parametersrsplit() Return ValueExample 1: How rsplit() works in Python?Example 2: How split() works when maxsplit is specified? The Python String rsplit() method is a…
View Post
Python String Istitle()

Python String istitle()

Table of Contents Hide What is titlecase?istitle() Syntaxistitle() Parameters istitle() Return valueExample 1: Python string istitle() working examplesExample 2: Program to check if the string is titlecase Python string istitle() method…
View Post
Python Jsonpath

Python JSONPath

Table of Contents Hide JSONPath Library in PythonInstalling jsonpath-ng ModuleJsonpath operators:Parsing a Simple JSON Data using JSONPathParsing a Json Array using JSONPath Expression JSONPath is an expression language that is…
View Post
Python Check If String Is Empty Or Not

Check if string is empty or not in Python

Table of Contents Hide Python program to check if string is empty or notUsing len() funtionUsing not operatorUsing not+str.strip() method Using not + str.isspace method In this article, you will learn…
View Post