简体   繁体   English

使用格式在python中格式化字符串

[英]formatting string in python using format

There is code: 有代码:

xor = 127002634777471167503839844242873650079
print '{0:x}'.format(xor)
print format(xor, 'x')

Can anyone explain what two last strings means? 谁能解释一下最后两个字符串的含义? How 5f8bd156f9e50e5381ab282ba2000b9f is generated from xor variable? 如何从xor变量生成5f8bd156f9e50e5381ab282ba2000b9f?

That's because this is the Hexa representation of your number. 这是因为这是您电话号码的十六进制表示形式。

You can set it to .2f (double in normal (fixed-point) notation), for example: 您可以将其设置为.2f (标准(定点)表示法的两倍),例如:

xor = 127002634777471167503839844242873650079
print '{0:.2f}'.format(xor)
print format(xor, '.2f')

Or using .2g (double in either normal or exponential notation, This type differs slightly from fixed-point notation in that insignificant zeroes to the right) 或使用.2g (以标准或指数表示法加倍,此类型与定点表示法稍有不同,因为右边的微不足道的零)

print '{0:.2g}'.format(xor)

You can read more about the type field ( .2f and .3g ) in here 您可以在此处阅读有关类型字段( .2f.3g )的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM