The Wayback Machine - https://web.archive.org/web/20240710011841/https://www.geeksforgeeks.org/python-string-isidentifier-method/
Open In App

Python String isidentifier() Method

Last Updated : 09 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Python String isidentifier() method is used to check whether a string is a valid identifier or not. The method returns True if the string is a valid identifier, else returns False.

Python String isidentifier() method Syntax

Syntax:  string.isidentifier()

Parameters: The method does not take any parameters

Return Value:  The method can return one of the two values: 

  • True: When the string is a valid identifier.
  • False: When the string is not a valid identifier.

Python String isidentifier() Method Example

Python3




string = "Coding_101"
print(string.isidentifier())


Output:

True

Note: A string is considered as a valid identifier if:

  • It only consists of alphanumeric characters and underscore (_)
  • Doesn’t start with a space or a number

Example 1: How isidentifier() works

Here, we have added some basic examples of using isidentifier() Method in Python

Python3




# String with spaces
string = "Geeks for Geeks"
print(string.isidentifier())
 
# A Perfect identifier
string = "GeeksforGeeks"
print(string.isidentifier())
 
# Empty string
string = ""
print(string.isidentifier())
 
# Alphanumerical string
string = "Geeks0for0Geeks"
print(string.isidentifier())
 
# Beginning with an integer
string = "54Geeks0for0Geeks"
print(string.isidentifier())


Output: 

False
True
False
True
False

Example 2: Using Python String isidentifier() Method in condition checking

Since, Python String isidentifier() returns boolean we can directly use isidentifier() inside if-condition to check if a String is an indicator or not.

Python3




string = "geeks101"
if string.isidentifier():
    print(string, "is an identifier.")
else:
    print(string, "is not an identifier.")
 
string = "GeeksForGeeks"
if string.isidentifier():
    print(string, "is an identifier.")
else:
    print(string, "is not an identifier.")
 
string = "admin#1234"
if string.isidentifier():
    print(string, "is an identifier.")
else:
    print(string, "is not an identifier.")
 
string = "user@12345"
if string.isidentifier():
    print(string, "is an identifier.")
else:
    print(string, "is not an identifier.")


Output:

geeks101 is an identifier.
GeeksForGeeks is an identifier.
admin#1234 is not an identifier.
user@12345 is not an identifier.


Previous Article
Next Article

Similar Reads

Class Method vs Static Method vs Instance Method in Python
Three important types of methods in Python are class methods, static methods, and instance methods. Each serves a distinct purpose and contributes to the overall flexibility and functionality of object-oriented programming in Python. In this article, we will see the difference between class method, static method, and instance method with the help o
5 min read
String slicing in Python to check if a string can become empty by recursive deletion
Given a string “str” and another string “sub_str”. We are allowed to delete “sub_str” from “str” any number of times. It is also given that the “sub_str” appears only once at a time. The task is to find if “str” can become empty by removing “sub_str” again and again. Examples: Input : str = "GEEGEEKSKS", sub_str = "GEEKS" Output : Yes Explanation :
2 min read
String slicing in Python to Rotate a String
Given a string of size n, write functions to perform following operations on string. Left (Or anticlockwise) rotate the given string by d elements (where d <= n).Right (Or clockwise) rotate the given string by d elements (where d <= n).Examples: Input : s = "GeeksforGeeks" d = 2Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeek
3 min read
Python | Sorting string using order defined by another string
Given two strings (of lowercase letters), a pattern and a string. The task is to sort string according to the order defined by pattern and return the reverse of it. It may be assumed that pattern has all characters of the string and all characters in pattern appear only once. Examples: Input : pat = "asbcklfdmegnot", str = "eksge" Output : str = "g
2 min read
String Alignment in Python f-string
Text Alignment in Python is useful for printing out clean formatted output. Some times the data to be printed varies in length which makes it look messy when printed. By using String Alignment the output string can be aligned by defining the alignment as left, right or center and also defining space (width) to reserve for the string. Approach : We
2 min read
String to Int and Int to String in Python
Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing the information about converting a string to int and int to string. Converting a string to an int If we want to convert a number that is represented in the string to int, we have to use the int() function. This function is used
2 min read
Pad or fill a string by a variable in Python using f-string
f-string stands for formatted string. It had come up by Python Version 3.6 and rapidly used to do easy formatting on strings. F-string is a string literal having syntax starts with f and followed by {}. That placeholder used for holding variable, that will be changed upon the variable names and their values respectively. There are already strings f
4 min read
Python string length | len() function to find string length
The string len() function returns the length of the string. In this article, we will see how to find the length of a string using the string len() method. Example: C/C++ Code string = "Geeksforgeeks" print(len(string)) Output13String len() Syntax len(string) Parameter String: string of which you want to find the length. Return: It returns
3 min read
Python String Formatting - How to format String?
String formatting allows you to create dynamic strings by combining variables and values. In this article, we will discuss about 5 ways to format a string. You will learn different methods of string formatting with examples for better understanding. Let's look at them now! How to Format Strings in PythonThere are five different ways to perform stri
8 min read
Convert Unicode String to a Byte String in Python
Python is a versatile programming language known for its simplicity and readability. Unicode support is a crucial aspect of Python, allowing developers to handle characters from various scripts and languages. However, there are instances where you might need to convert a Unicode string to a regular string. In this article, we will explore five diff
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg