new string formatting with local variables

J

Jabba Laci

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
 
S

Steve Crook

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)
 
E

Ethan Furman

Steve said:
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~
 
P

Prasad, Ramit

print "{} was captured by {}".format(solo, jabba)
Is this Python2.7 specific?

Python 2.6.x :
ValueError: zero length field name in format


Ramit




This communicationis 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 orhard
copy format. Thank you.

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

Ethan Furman

Is this Python2.7 specific?

Python 2.6.x :
ValueError: zero length field name in format

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

~Ethan~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top