python中的前导零 [英] leading zeros in python
本文介绍了python中的前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Python 似乎能够接受除 08 或 09 之外的任何数字的前导零.例如,
Python seems to be able to accept leading zeros for any number except 08 or 09. For instance,
a = 04
在解释器中工作,但
a = 08
返回
SyntaxError: invalid token
我在 OSX 上使用 python 2.7.3,其他人已经能够复制错误.什么给?
I'm using python 2.7.3 on OSX, and others have been able to duplicate the error. What gives?
推荐答案
带有前导零的数字被解释为八进制,其中数字 8
和 9
不不存在.
Numbers with a leading zero in them are interpreted as octal, where the digits 8
and 9
don't exist.
在 Python 3 中情况更糟,无论您使用哪个数字,前导零都是语法错误.请参阅新八进制文字"下的 Python 3.0 中的新功能.还有 PEP 3127.
It's worse in Python 3, leading zeros are a syntax error no matter which digits you use. See What’s New In Python 3.0 under "New octal literals". Also PEP 3127.
这篇关于python中的前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文