Python String isidentifier() 方法
Python String isidentifier()方法用于检查字符串是否为有效标识符。如果字符串是有效标识符,则该方法返回 True,否则返回 False。
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.
示例:isidentifier() 如何工作
Python3
# Python code to illustrate
# the working of isidentifier()
# 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())
输出:
False
True
False
True
False