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

Frank Millman frank at chagford.com
Mon Jan 19 04:26:30 EST 2015


"contro opinion" <contropinion at gmail.com> wrote in message 
news:CA+YdQ_651X0NDPw1j203WGbeDTxy_Mw7g0w3VH1WoaGR1iv-Cg at mail.gmail.com...
> In the python3 console:
>
>    >>> a=18
>    >>> b='18'
>    >>> str(a) == b
>    True
>    >>> int(b) == a
>    True
>
>
> 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'
>
I am no expert, but does this answer your question ?

C:\>python
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.


>>> a1 = 0xf4
>>> a1
244
>>> b1 = '0x{:x}'.format(a1)
>>> b1
'0xf4'
>>> int(b1, 16)
244


>>> a2 = 0o36
>>> a2
30
>>> b2 = '0o{:o}'.format(a2)
>>> b2
'0o36'
>>> int(b2, 8)
30


>>> a3 = 011
  File "<stdin>", line 1
    a3 = 011
           ^
SyntaxError: invalid token
>>>

Frank Millman








More information about the Python-list mailing list