Python String isidentifier() Method with Examples

In the previous article, we have discussed Python Program to Find Index of a Tuple Item
isidentifier() Method in Python:

If the string is a valid identifier, the isidentifier() method returns true; otherwise, it returns False.

If a string only contains alphanumeric letters (a-z) and numbers (0-9), it is considered a valid identifier. A valid identifier must not begin with a number and must not contain any spaces.

Syntax:

string.isidentifier()

Parameters: This function has no parameters.

Return Value:

The isidentifier() method gives:

  • If the string is a valid identifier, it returns true.
  • If the string is not an invalid identifier, it returns False.

Examples:

Example1:

Input:

Given first string = 'PythonPrograms'
Given second string = 'hello 123'
Given third string = '123welcome'
Given fourth string = ''

Output:

The given string =  PythonPrograms
Checking if the given string is a valid identifier or not =  True
The given string =  hello 123
Checking if the given string is a valid identifier or not =  False
The given string =  123welcome
Checking if the given string is a valid identifier or not =  False
The given string =  
Checking if the given string is a valid identifier or not =  False

Example2:

Input:

Given first string = 'Btechgeeks'
Given second string = 'Programs_python'
Given third string = 'welcome@btechgeeks'
Given fourth string = 'goodmorning123456'

Output:

The given string = Btechgeeks
Checking if the given string is a valid identifier or not = True
The given string = Programs_python
Checking if the given string is a valid identifier or not = True
The given string = welcome@btechgeeks
Checking if the given string is a valid identifier or not = False
The given string = goodmorning123456
Checking if the given string is a valid identifier or not = True

String isidentifier() Method Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Apply isidentifier() function to the given string that returns true if the given string is a valid identifier,  otherwise, it returns False.
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string is a valid identifier or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_strng1 = 'PythonPrograms'
# Apply isidentifier() function to the given string that returns true if
# the given string is a valid identifier,  otherwise, it returns False.
# Store it in another variable.
rslt_1 = gvn_strng1.isidentifier()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string is a valid
# identifier or not.
print("Checking if the given string is a valid identifier or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 = 'hello 123'
rslt_2 = gvn_strng2.isidentifier()
print("The given string = ", gvn_strng2)
print("Checking if the given string is a valid identifier or not = ", rslt_2)

gvn_strng3 = '123welcome'
rslt_3 = gvn_strng3.isidentifier()
print("The given string = ", gvn_strng3)
print("Checking if the given string is a valid identifier or not = ", rslt_3)

gvn_strng4 = ''
rslt_4 = gvn_strng4.isidentifier()
print("The given string = ", gvn_strng4)
print("Checking if the given string is a valid identifier or not = ", rslt_4)

Output:

The given string =  PythonPrograms
Checking if the given string is a valid identifier or not =  True
The given string =  hello 123
Checking if the given string is a valid identifier or not =  False
The given string =  123welcome
Checking if the given string is a valid identifier or not =  False
The given string =  
Checking if the given string is a valid identifier or not =  False

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Apply isidentifier() function to the given string that returns true if the given string is a valid identifier,  otherwise, it returns False.
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string is a valid identifier or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_strng1 = input("Enter some Random String = ")
# Apply isidentifier() function to the given string that returns true if
# the given string is a valid identifier,  otherwise, it returns False.
# Store it in another variable.
rslt_1 = gvn_strng1.isidentifier()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string is a valid
# identifier or not.
print("Checking if the given string is a valid identifier or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 = input("Enter some Random String = ")
rslt_2 = gvn_strng2.isidentifier()
print("The given string = ", gvn_strng2)
print("Checking if the given string is a valid identifier or not = ", rslt_2)

gvn_strng3 = input("Enter some Random String = ")
rslt_3 = gvn_strng3.isidentifier()
print("The given string = ", gvn_strng3)
print("Checking if the given string is a valid identifier or not = ", rslt_3)

gvn_strng4 = input("Enter some Random String = ")
rslt_4 = gvn_strng4.isidentifier()
print("The given string = ", gvn_strng4)
print("Checking if the given string is a valid identifier or not = ", rslt_4)

Output:

Enter some Random String = Btechgeeks
The given string = Btechgeeks
Checking if the given string is a valid identifier or not = True
Enter some Random String = Programs_python
The given string = Programs_python
Checking if the given string is a valid identifier or not = True
Enter some Random String = welcome@btechgeeks
The given string = welcome@btechgeeks
Checking if the given string is a valid identifier or not = False
Enter some Random String = goodmorning123456
The given string = goodmorning123456
Checking if the given string is a valid identifier or not = True

Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.