Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.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; '*args,': 0.07; 'braces': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:method': 0.09; 'subject:string': 0.09; 'def': 0.10; 'skip:f 30': 0.15; '"<>"': 0.16; '42,': 0.16; 'braces,': 0.16; 'characters:': 0.16; 'circumvent': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:raw': 0.16; 'string': 0.17; 'wrote:': 0.17; 'hack': 0.18; 'import': 0.21; "i'd": 0.22; 'header:User-Agent:1': 0.26; 'replace': 0.27; 'header:X-Complaints-To:1': 0.28; 'this?': 0.28; 'skip:( 20': 0.28; 'probably': 0.29; 'class': 0.29; 'print': 0.32; 'skip:s 30': 0.33; 'to:addr:python-list': 0.33; 'thanks': 0.34; 'subject:?': 0.35; 'there': 0.35; 'received:org': 0.36; 'method': 0.36; 'anything': 0.36; 'uses': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'here': 0.65; 'therefore': 0.65; 'hardly': 0.84; 'tex': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: raw format string in string format method? Date: Thu, 28 Feb 2013 16:41:54 +0100 Organization: None References: <512f6585$0$3108$ba620e4c@news.skynet.be> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849ea1.dip.t-dialin.net User-Agent: KNode/4.7.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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362066115 news.xs4all.nl 6850 [2001:888:2000:d::a6]:43399 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40134 Helmut Jarausch wrote: > I'd like to print a string with the string format method which uses > {0}, ... > > Unfortunately, the string contains TeX commands which use lots of > braces. Therefore I would have to double all these braces just for the > format method which makes the string hardly readable. > > Is there anything like a "raw" format string and any other means > to circumvent this? > > Many thanks for a hint, > Helmut. While percent-formatting is probably the way to go here is a hack to replace the braces with custom characters: import string class SwapFormatter(string.Formatter): custom = "<>" braces = "{}" swap = {ord(p): q for p, q in zip(custom + braces, braces + custom)} def format(self, format_string, *args, **kw): format_string = format_string.translate(self.swap) return super().format(format_string, *args, **kw) def parse(self, format_string): return ((t[0].translate(self.swap),) + t[1:] for t in super().parse(format_string)) myformat = SwapFormatter().format print(myformat("<< {} {{<0>}} <2> {} <1:> >>", "alpha", 42, "gamma", foo="delta", width=5))