[Python-3000] String formating operations in python 3k

Nick Coghlan ncoghlan at gmail.com
Mon Apr 3 15:43:17 CEST 2006


Barry Warsaw wrote:
> On Sun, 2006-04-02 at 23:26 -0500, Ian Bicking wrote:
>> Georg Brandl wrote:
>>> BTW, has anyone seen string.Template being used somewhere?
>> I use it from time to time, usually when formatting user-provided 
>> strings (because "%(foo)s" is not very pleasant or easy-to-explain 
>> compared to "$foo").  However, I *never* use it internally in code, 
>> because the overhead of importing string and creating the template far 
>> outweighs the inconvenience of %-based substitution.  Oh, and I really 
>> like %r.
> 
> Good point.  Yes string.Template was designed primarily for i18n
> applications for the reasons above, and because %(foo)s is more error
> prone than necessary.
> 
>> I'd personally be very happy if $"$foo" worked, as well as 
>> "$foo".substitute().  $/shell-style substitution is the norm out in the 
>> world, not the %/printf style that Python uses; it'd be nice to move 
>> towards that norm.  string.Template is a bit of a tease that way, since 
>> it doesn't actually provide a very convenient alternative to the bulk of 
>> what % is used for.
> 
> I don't much like the $"" prefix, but I agree that it would be nicer if
> there were more direct support for $-strings.  OTOH, I don't see a good
> way to marry the rich coercion of %-substitution with the simplicity of
> $-substition.  I wouldn't want to lose that simplicity to gain that
> richness in $-strings, so I suppose a prefix might be necessary.

What do you think of a "format" builtin function that accepts the format as 
the first argument (similar to printf).

The version on my harddrive permits positional arguments via $1, $2, etc, as 
well as string formatting (by sticking the format code in square brackets 
between the $ and the identifier). Keyword arguments still work, naturally.

And if you don't want formatting, you just leave out the square brackets.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list