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


Groups > comp.lang.python > #7091

new string formatting with local variables

From Jabba Laci <jabba.laci@gmail.com>
Date 2011-06-06 12:15 -0400
Subject new string formatting with local variables
Newsgroups comp.lang.python
Message-ID <mailman.2490.1307376958.9059.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

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

csiph-web