显示一个八进制值作为其字符串表示形式 [英] display an octal value as its string representation

查看:50
本文介绍了显示一个八进制值作为其字符串表示形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将八进制数转换为字符串时遇到问题.

I've got a problem when converting an octal number to a string.

p = 01212
k = str(p)
print k

结果是 650 ,但是我需要 01212 .我怎样才能做到这一点?预先感谢.

The result is 650 but I need 01212. How can I do this? Thanks in advance.

推荐答案

您的数字 p 是实际的,而不是那个价值.因此实际上是 650 10 1212 8 28a 16 全部同时显示.

Your number p is the actual value rather than the representation of that value. So it's actually 65010, 12128 and 28a16, all at the same time.

如果您希望将其视为八进制,只需使用:

If you want to see it as octal, just use:

print oct(p)

按照以下记录:

>>> p = 01212
>>> print p
650
>>> print oct(p)
01212

这是针对Python 2的(您似乎正在使用它,因为您使用的是八进制文字的 0NNN 变体,而不是 0oNNN ).

That's for Python 2 (which you appear to be using since you use the 0NNN variant of the octal literal rather than 0oNNN).

Python 3的表示形式略有不同:

Python 3 has a slightly different representation:

>>> p = 0o1212
>>> print (p)
650
>>> print (oct(p))
0o1212

这篇关于显示一个八进制值作为其字符串表示形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆