Python 字符串 isidentifier()

isidentifier() 的语法是

string.isidentifier()

isidentifier() 参数

isidentifier() 方法不接受任何参数。


isidentifier() 的返回值

isidentifier() 方法返回

  • True 如果 字符串 是有效的 标识符
  • False 如果字符串不是无效的标识符

示例 1:isidentifier() 如何工作?

str = 'Python'
print(str.isidentifier())

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

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

str = ''
print(str.isidentifier())

输出

True
False
False
False

访问此页面以了解 Python 中什么是有效标识符?


示例 2: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.')

输出

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

另请阅读

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战