Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #84007

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

From "Frank Millman" <frank@chagford.com>
Subject Re: How to change the number into the same expression's string and vice versa?
Date 2015-01-19 11:26 +0200
References <CA+YdQ_651X0NDPw1j203WGbeDTxy_Mw7g0w3VH1WoaGR1iv-Cg@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.17847.1421659596.18130.python-list@python.org> (permalink)

Show all headers | View raw


"contro opinion" <contropinion@gmail.com> wrote in message 
news:CA+YdQ_651X0NDPw1j203WGbeDTxy_Mw7g0w3VH1WoaGR1iv-Cg@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




Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: How to change the number into the same expression's string and vice versa? "Frank Millman" <frank@chagford.com> - 2015-01-19 11:26 +0200

csiph-web