我是靠谱客的博主 眯眯眼裙子,这篇文章主要介绍python去掉数字第一个0_从字符串转换为int时,为什么Python会删除前导零?,现在分享给大家,希望可以做个参考。

>>> num = int('0241034812')

>>> print(num)

241034812

>>>

In the above code, I'm setting the variable num to 0241034812; however, when I print the variable it deletes the first zero.

Why is this happening?

解决方案

Integers are stored/displayed as a "normal" number, not the string value. If you want to display the number prefixed with zeroes you can do that when displaying the variable.

You can use the following syntax to add leading zeros to an integer:

print "%010d" % (241034812,)

The example above will display the integer 241034812 with 10 digits as 0241034812.

最后

以上就是眯眯眼裙子最近收集整理的关于python去掉数字第一个0_从字符串转换为int时,为什么Python会删除前导零?的全部内容,更多相关python去掉数字第一个0_从字符串转换为int时内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(117)

评论列表共有 0 条评论

立即
投稿
返回
顶部