English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Python Functions

Python Data Types

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python string isidentifier() usage and example

Python string methods

If the string is a valid identifier in Python, the isidentifier() method returns True. If not, it returns False.

The syntax of isidentifier() is:

string.isidentifier()

isidentifier() parameters

The isidentifier() method does not take any parameters.

isidentifier() return value

The isidentifier() method returns:

  • True if the string is a valid identifier

  • False if the string is not a valid identifier

Example1: How does isidentifier() work?

str = 'Python'
print(str.isidentifier())
str = 'Py thon'
print(str.isidentifier())
str = ''22Python
print(str.isidentifier())
str = ''
print(str.isidentifier())

When running the program, the output is:

True
False
False
False

Visit this page to learnWhat is a valid identifier in Python?

Example2: More examples 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.')

When running the program, the output is:

root33 Is a valid identifier.
33root is not a valid identifier.
root 33 Is not a valid identifier.

Python string methods