Python String isidentifier() 方法用於檢查字符串是否為有效標識符。如果字符串是有效標識符,則該方法返回 True,否則返回 False。
用法:
string.isidentifier()
參數:
該方法不帶任何參數
返回值:
該方法可以返回以下兩個值之一:
- True:當字符串是有效標識符時。
- False:當字符串不是有效標識符時。
示例: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
相關用法
- Python String casefold()用法及代碼示例
- Python String center()用法及代碼示例
- Python String count()用法及代碼示例
- Python String endswith()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 Python String isidentifier() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。