new string formatting with local variables

Ethan Furman ethan at stoneleaf.us
Mon Jun 6 14:57:36 EDT 2011


Steve Crook wrote:
> On Mon, 6 Jun 2011 12:15:35 -0400, Jabba Laci wrote in
> Message-Id: <mailman.2490.1307376958.9059.python-list at 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~



More information about the Python-list mailing list