Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python String center() Method
Next article icon

Theisidentifier() method inPython is used to check whether a givenstring qualifies as a valid identifier according to the Python language rules. Identifiers are names used to identify variables, functions, classes, and other objects. A valid identifier must begin with a letter (A-Z or a-z) or an underscore (_), should contain only alphanumeric characters (A-Z, a-z, 0-9) and underscores (_), and should not be a reserved keyword in Python. In this article, we will see how theisidentifier() method works:

Let's understand with the help of an example:

Python
s="variable_name"print(s.isidentifier())

Output
True

Explanation:

  • The string"variable_name" follows all the rules of a valid Python identifier:
    • It contains only letters, digits, and underscores.
    • It does not start with a digit.
    • It is not a Python keyword.
  • Hence, the method returnsTrue.

Syntax of isidentifier()

string.isidentifier()

Parameters

  • Theisidentifier() method does not take any parameters.

Return Type

  • ReturnsTrue if the string is a valid identifier.
  • ReturnsFalse otherwise.

Examples of isidentifier method()

1. Starting with a number

Anidentifier cannot start with a number.

Python
# Starts with a numbers="1variable"print(s.isidentifier())

Explanation:

  • The string"1variable" begins with the digit1, which violates the rules for identifiers.
  • Consequently, the method returnsFalse.

2. Containing special characters

Special characters, apart from the underscore, are not allowed in identifiers.

Python
s="var!name"# Contains a special character (!)print(s.isidentifier())

Explanation:

  • The string"var!name" includes the special character!, making it an invalid identifier.
  • The method appropriately returnsFalse.

3. Reserved keywords

Python’sreserved keywordscannot be used as identifiers. While they follow the naming rules, their special meaning in the language makes them invalid as identifiers:

Python
s="class"# Reserved keyword in Pythonprint(s.isidentifier())

Explanation:

  • The string"class" follows all identifier rules, soisidentifier() returnsTrue.
  • However, using it as an identifier in code will lead to aSyntaxError because it is a reserved keyword.

4. Empty string

An empty string cannot qualify as an identifier:

Python
s=""# Empty stringprint(s.isidentifier())

Explanation:

  • The string is empty, so it cannot meet the rules for identifiers.
  • The method returnsFalse accordingly.

Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp