Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!takemy.news.telefonica.de!telefonica.de!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string.': 0.05; 'python3': 0.07; 'subject:same': 0.07; 'literal': 0.09; 'prefix': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; 'subject:number': 0.09; 'subject:string': 0.09; 'valueerror:': 0.09; 'subject:How': 0.10; 'python': 0.11; '16)': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'recognised': 0.16; 'subject:expression': 0.16; 'wrote:': 0.18; 'passing': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'specify': 0.24; 'header:X-Complaints-To:1': 0.27; '"",': 0.31; 'file': 0.32; 'linux': 0.33; '(most': 0.33; 'subject:the': 0.34; 'add': 0.35; 'vice': 0.36; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'numbers': 0.61; 'more': 0.64; 'invalid': 0.68; '2014,': 0.84; 'recognition': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: How to change the number into the same expression's string and vice versa? Date: Mon, 19 Jan 2015 10:24:17 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd89f4.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421659473 news.xs4all.nl 2893 [2001:888:2000:d::a6]:36473 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84006 contro opinion wrote: > 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' 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. You can specify the base for int() or trigger automatic base recognition by passing a base of 0: >>> int("f4", 16) 244 >>> int("0xf4", 0) 244 >>> int("0o36", 0) 30 The classical prefix for base-8 numbers is no longer recognised in Python 3: >>> int("011", 0) Traceback (most recent call last): File "", line 1, in ValueError: invalid literal for int() with base 0: '011' >>> int("011") 11 >>> int("011", 8) 9