What is this syntax ?

Benjamin Kaplan benjamin.kaplan at case.edu
Sun Jun 19 18:29:41 EDT 2011


On Sun, Jun 19, 2011 at 2:06 PM, Vito 'ZeD' De Tullio
<zak.mc.kraken at libero.it> wrote:
> Roy Smith wrote:
>
>> There's something nice about building up strings in-line, as
>> opposed to having to look somewhere to see what's being interpolated.
>> To give a more complex example, consider:
>>
>> print "$scheme://$host:$port/$route#$fragment"
>>
>> That certainly seems easier to me to read than:
>>
>> print "%s://%s:%s/%s#%s" % (scheme,
>>                             port,
>>                             host,
>>                             route,
>>                             fragment)
>>
>> because I don't have to line up the nth format specifier with the nth
>> data item.
>
> well, in python3 you can use dict to format strings
>
>>>> print("%(a)s" % {'a':'b'})
> b
>
> and you can achieve php interpolation via locals()
>
>>>> a = 'b'
>>>> print("%(a)s" % locals())
> b
>
>
> --
> By ZeD

That's a lot older than Python 3. Here's the example from the 2.3
docs:  http://docs.python.org/release/2.3/lib/typesseq-strings.html

Python 3 added a different syntax for string formatting, using .NET's
formatting syntax instead of C's.

"{scheme}://{host}:{port}/{route}#{fragment}".format(locals())



More information about the Python-list mailing list