How to change the number into the same expression's string and vice versa?

Jussi Piitulainen jpiitula at ling.helsinki.fi
Mon Jan 19 04:36:50 EST 2015


Peter Otten writes:
> contro opinion wrote:

> > Now how to change a1,a2,a3  into b1,b2,b3 and vice versa?
> > a1=0xf4
> > a2=0o36
> > a3=011
> > 
> > b1='0xf4'
> > b2='0o36'
> > b3='011'
> 
> Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
> [GCC 4.8.2] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> "{:o}".format(0xf4)
> '364'
> >>> "{:x}".format(0xf4)
> 'f4'
> >>> "{}".format(0xf4)
> '244'
> 
> To add a prefix just put it into the format string.

There's also these (in Python 3.2.3):

  >>> hex(0xf4)
  '0xf4'
  >>> oct(0xf4)
  '0o364'
  >>> bin(0xf4)
  '0b11110100'



More information about the Python-list mailing list