Search code examples
pythonstringformatmd5sum

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?


Solution

  • 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:

    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)

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

    You can read more about the type field (.2f and .3g) in here