Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'though:': 0.07; 'string': 0.09; 'dependency': 0.09; 'occurrences': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'happy.': 0.16; 'hell.': 0.16; 'nesting': 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; 'skip:{ 30': 0.16; 'subject:format': 0.16; '{0}': 0.16; '{1},': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'replace': 0.24; 'skip:{ 20': 0.24; 'header:X-Complaints-To:1': 0.27; 'converting': 0.30; 'way?': 0.31; 'file': 0.32; 'could': 0.34; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'most': 0.60; 'numbers': 0.61; 'first': 0.61; 'of:': 0.68; 'clearer': 0.84; 'subject:Best': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Best way to use globally format Date: Sun, 03 May 2015 18:16:41 +0200 Organization: None References: <87wq0pk7pf.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd888f.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430669817 news.xs4all.nl 2946 [2001:888:2000:d::a6]:42616 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89879 Cecil Westerhof wrote: > I have a file where I used a lot of {0}, {1} and {2}. Most but not all > are changed to {0:.3E}, {1:.3E} and {2:.3E}. But when I want to change > the format I come in dependency hell. > > I could do something like: > format = ':.3E' > fmt0 = '{0' + format + '} > fmt1 = '{1' + format + '} > fmt2 = '{2' + format + '} > > and replace occurrences of: > 'before {0} after' > with: > 'before ' + fmt0 + ' after' > > But that does not really make me happy. Is there a better way? There's limited support for nesting {...}: >>> "{0:{fmt}}, {1:{fmt}}, {2:{fmt}}".format(1.12345789, 2., 3., fmt=".3E") '1.123E+00, 2.000E+00, 3.000E+00' >>> "{0:{fmt}}, {1:{fmt}}, {2:{fmt}}".format(1.12345789, 2., 3., fmt="6.2") ' 1.1, 2.0, 3.0' >>> "{0:{fmt}}, {1:{fmt}}, {2:{fmt}}".format(1.12345789, 2., 3., fmt="06.2") '0001.1, 0002.0, 0003.0' Converting the numbers to string first may be clearer though: >>> formatted_numbers = [format(x, "010.2") for x in [1.12345789, 2., 3.]] >>> "{0}, {1}, {2}".format(*formatted_numbers) '00000001.1, 00000002.0, 00000003.0'