【发布时间】:2021-02-27 17:04:51
【问题描述】:
我在 Python 3.8 和 2.7 中都搞砸了,发现 Python 3 中的 print 函数不允许在 print 中使用前导零。见下文:
>>> print(01)
File "<stdin>", line 1
print(01)
^
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers
我想这是因为 Python 3.x 即使在打印时也会区分数据类型,这就是以下工作的原因:
>>> print('01')
01
我明确要求打印一个字符串。尽管在 Python 2.7 中,以下语句没有错误:
>>> print '01'
01
它只是返回我问的内容。是不是意味着 Python 2.x 总是将打印值转换为字符串?
【问题讨论】:
-
除了第一种情况外,您似乎在值周围加上引号......
标签: python types printf python-2.x