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

Talin talin at acm.org
Wed Apr 5 21:17:37 CEST 2006


Greg Ewing <greg.ewing <at> canterbury.ac.nz> writes:

> I'm also not all that keen on $, either inside
> or outside the string. It seems to me that
> something like
> 
>    "User {user} has printed {n} pages"

FWIW, this is very similar to the syntax used in the .Net function String.Format
(I think Java uses a similar syntax as well). I don't think it supports
named args, though.

In fact, a lot of the proposals in the followup postings are eerily similar
to the .Net syntax for formatting options.) So for those who are concerned
with the ease of migrating from other languages, well, it really depends on
which set of users you are interested in :)

Here's a quick summary:

"{0}"  -- positional arg
"{{some text}}" -- escaped braces
"{0:x8}" -- format as 8-digit hexadecimal

String.Format(”{0:(###) ###-####}”, 8005551212)
   ==> "(800) 555-1212"

Here's a link to a reference page for the various formatting options:

http://blog.stevex.net/index.php/string-formatting-in-csharp/

In general, I would be very much in favor of a convention that allows mixed
positional and named arguments. As much as I like the % operator, I find
that the "all or nothing" leads to some awkwardness in practice.

-- Talin



More information about the Python-3000 mailing list