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


Groups > comp.lang.python > #7091 > unrolled thread

new string formatting with local variables

Started byJabba Laci <jabba.laci@gmail.com>
First post2011-06-06 12:15 -0400
Last post2011-06-06 14:07 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  new string formatting with local variables Jabba Laci <jabba.laci@gmail.com> - 2011-06-06 12:15 -0400
    Re: new string formatting with local variables Steve Crook <steve@mixmin.net> - 2011-06-06 18:30 +0000
      Re: new string formatting with local variables Ethan Furman <ethan@stoneleaf.us> - 2011-06-06 11:57 -0700
      RE: new string formatting with local variables "Prasad, Ramit" <ramit.prasad@jpmchase.com> - 2011-06-06 16:21 -0400
      Re: new string formatting with local variables Ethan Furman <ethan@stoneleaf.us> - 2011-06-06 14:07 -0700

#7091 — new string formatting with local variables

FromJabba Laci <jabba.laci@gmail.com>
Date2011-06-06 12:15 -0400
Subjectnew string formatting with local variables
Message-ID<mailman.2490.1307376958.9059.python-list@python.org>
Hi,

I'd like to simplify the following string formatting:

solo = 'Han Solo'
jabba = 'Jabba the Hutt'
print "{solo} was captured by {jabba}".format(solo=solo, jabba=jabba)
# Han Solo was captured by Jabba the Hutt

What I don't like here is this: "solo=solo, jabba=jabba", i.e. the
same thing is repeated. In "solo=solo", the left part is a key and the
right part is the value of a local variable, but it looks strange.

I'd like something like this:
print "{solo} was captured by {jabba}".format(locals())        # WRONG!

But it doesn't work.

Do you have any idea?

Thanks,

Laszlo

[toc] | [next] | [standalone]


#7104

FromSteve Crook <steve@mixmin.net>
Date2011-06-06 18:30 +0000
Message-ID<slrniuq75p.47d.steve@news.mixmin.net>
In reply to#7091
On Mon, 6 Jun 2011 12:15:35 -0400, Jabba Laci wrote in
Message-Id: <mailman.2490.1307376958.9059.python-list@python.org>:

> solo = 'Han Solo'
> jabba = 'Jabba the Hutt'
> print "{solo} was captured by {jabba}".format(solo=solo, jabba=jabba)
> # Han Solo was captured by Jabba the Hutt

How about:-

print "%s was captured by %s" % (solo, jabba)

[toc] | [prev] | [next] | [standalone]


#7106

FromEthan Furman <ethan@stoneleaf.us>
Date2011-06-06 11:57 -0700
Message-ID<mailman.2502.1307385872.9059.python-list@python.org>
In reply to#7104
Steve Crook wrote:
> On Mon, 6 Jun 2011 12:15:35 -0400, Jabba Laci wrote in
> Message-Id: <mailman.2490.1307376958.9059.python-list@python.org>:
> 
>> solo = 'Han Solo'
>> jabba = 'Jabba the Hutt'
>> print "{solo} was captured by {jabba}".format(solo=solo, jabba=jabba)
>> # Han Solo was captured by Jabba the Hutt
> 
> How about:-
> 
> print "%s was captured by %s" % (solo, jabba)

Or even

print "{} was captured by {}".format(solo, jabba)

or how about

print "{victim} was captured by {captor}".format(
         victim=solo, captor=jabba)

or maybe

print "{hapless_twit} was captured by {mega_bad_dude}".format(
         hapless_twit=solo, mega_bad_dude=jabba)


~Ethan~

[toc] | [prev] | [next] | [standalone]


#7108

From"Prasad, Ramit" <ramit.prasad@jpmchase.com>
Date2011-06-06 16:21 -0400
Message-ID<mailman.2505.1307391970.9059.python-list@python.org>
In reply to#7104
> print "{} was captured by {}".format(solo, jabba)
Is this Python2.7 specific?

Python 2.6.x : 
>>>print "{} was captured by {}".format('t1', 't2')
ValueError: zero length field name in format


Ramit




This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase &
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.

[toc] | [prev] | [next] | [standalone]


#7109

FromEthan Furman <ethan@stoneleaf.us>
Date2011-06-06 14:07 -0700
Message-ID<mailman.2506.1307393682.9059.python-list@python.org>
In reply to#7104
Prasad, Ramit wrote:
>> print "{} was captured by {}".format(solo, jabba)
> Is this Python2.7 specific?
> 
> Python 2.6.x : 
>>>> print "{} was captured by {}".format('t1', 't2')
> ValueError: zero length field name in format

Apparently it is 2.7 and greater -- my apologies for not specifying that.

~Ethan~

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web