Variables in strings..

Peter Schneider-Kamp peter at schneider-kamp.de
Mon Jul 24 08:40:31 EDT 2000


Gabriel Ambuehl wrote:
> 
> print "Hello $name"
> would do the job. Now the only solution I could find in Python was
> this awful
> print "Hello %(name)s" % var()

There is of course:

print "Hello", name
print "Hello", name[1]

But that is probably not what you ask for. There is a thread about
extending the print statement right now at the python-dev mailing list:
http://www.python.org/pipermail/python-dev/2000-July/thread.html

Part of this thread deals with what you are looking for.
You may want to read the whole thread, but a good point to start
is Tim's first post in that subthread:

http://www.python.org/pipermail/python-dev/2000-July/013895.html

> syntax which, after all, doesn't even seem to support lists as
> print "Hello %(name[1])s" % var()
> just raises a KeyError. May anyone point me to some more comfortable
> ways than just endless concatenation of strings and lists which doesn't really
> help for readibility (which is one of the main points why I'm
> switching from Perl to Python)?

As far as I have understood matters, the $name $(name[1]) syntax
woul be able to do what you want. But then I have not really
followed that thread closely.

Peter

P.S.: To python-dev: Wouldn't it be better to discuss this
                     on python-list (or pep-dev<wink>)?
--
Peter Schneider-Kamp          ++47-7388-7331
Herman Krags veg 51-11        mailto:peter at schneider-kamp.de
N-7050 Trondheim              http://schneider-kamp.de




More information about the Python-list mailing list