python符号%u和%o

时间:2016-11-09 21:07:04

标签: python octal

我是编程的新手,并试图弄清楚%符号在不同字母的打印语句中的作用。我理解几乎所有这些都接受了你所做的事情。看起来它只是将538打印为整数。我在印刷声明之前看到了一个' u'以unicode打印,但我不知道它是否适用于%u。

UserNameView.as_view(actions={'head': ...})

输出如下:

print "In honor of the election I present %d" % 538.0 # integer
print "In honor of the election I present %o" % 538.0 # octal
print "In honor of the election I present %u" % 538.0 # ?
print "In honor of the election I present %x" % 538.0 # lowercase hexadecimal
print "In honor of the election I present %X" % 538.0 # uppercase hexadecimal
print "In honor of the election I present %e" % 538.0 # exponential
print "In honor of the election I present %i" % 538.0 # integer

我对这个号码In honor of the election I present 538 In honor of the election I present 1032 *emphasized text* In honor of the election I present 538 *emphasized text* In honor of the election I present 21a In honor of the election I present 21A In honor of the election I present 5.380000e+02 In honor of the election I present 538 也有点麻烦。我刚刚学会了以八进制打印的内容,我认为它会输出%o132),但输出为538 --> 8^3 = 512 *(1) + 26, 8^1 = 8*(3) + 2, 8^0 = 1*(2)。 0来自哪里?

1 个答案:

答案 0 :(得分:4)

来自the docs%u

  

过时类型 - 与'd'相同。

%o告诉print538解释为base-10并将其转换为八进制。 538个基数10(538 10 )是八进制的1032(1032 8 ):

1 * 8^3 + 0 * 8^2 + 3 * 8^1 + 2 * 8^0
= 512 + 0 + 24 + 2
= 538

它显示1032,因为它们是8 n 的适当系数。 0对应于8 2 。如果你把它遗漏了,你有132 8 = 1 * 64 + 3 * 8 + 2 = 90 10 ,而不是538 10

所以,那里没什么奇怪的。