[Python-Dev] string formatting options and removing basestring.mod (WAS: Replacement for print in Python 3.0) (original) (raw)

Steven Bethard steven.bethard at gmail.com
Mon Sep 5 00:08:18 CEST 2005


[Raymond Hettinger]

Actually, formatting needs to become a function. The overloading of the arithmetic mod operator has proven to be unfortunate (if only because of precedence issues).

[Guido van Rossum]

For me, it's not so much the precedence, but the fact that "%s" % x doesn't work as expected if x is a tuple; you'd have to write "%s" % (x,) which is tedious.

[Raymond Hettinger]

Also, the format coding scheme itself needs to be revisited. There is no shortage of people who have taken issue with the trailing s in %(myvar)s. [snip[] string.Template is a bit too simplified. But perhaps it can be adapted. We still want some way to express %r, %6.2f, etc. Since string formatting has been around since Tim was in diapers, we should probably start by looking at the solutions used by other languages.

I was curious about what kind of options there were, so I googled around a bit. Here's what I found:

Java[1] uses syntax like: %[argument_index$][flags][width][.precision]conversion which is basically the same as that of C, with positional argument specifiers. Some examples: String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c); System.out.format("Local time: %tT", Calendar.getInstance()); formatter.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d") Classes can customize formatting for the 's' specifier by implementing the Formattable interface, which provides a method: formatTo(Formatter fmt, int f, int width, int precision) You can get formatted objects by calling:

.Net[2] uses syntax like: {index[,alignment][:formatString]} with examples like: String.Format("{0:dddd MMMM}", DateTime.Now) Console.WriteLine("{0:C}", MyInt) String.Format("Name = {0}, hours = {1:hh}, minutes = {1:mm}", myName, DateTime.Now) Classes can customize formatting for any specifier by implementing the ICustomFormatter interface: Format(string format, object arg, IFormatProvider formatProvider); or the IFormattable interface: ToString(string format, IFormatProvider formatProvider); You can get formatted objects by calling:

I also briefly looked at Tcl/Tk[3], Common Dylan[4], OCaml[5] and Ruby[6][7], which all appear to use C-style (or similar) formatting. I believe that Ruby, in addition to having printf and sprintf, also uses the % operator like Python does.

This was a pretty small sample of languages (just the first few that showed up in google), and I didn't really look at any of them other than Java and .Net in much depth, so I've may have misunderstood some of it. That said, I think it's probably pretty reasonable to conclude that C-style formatting is the choice of a lot of other languages. (Not to imply that it therefore needs to be the choice of Python.)

I understand one of the complaints about string formatting in Python is having to write the "s" on things like "%(key)s". I was hoping to get some ideas for alternatives here, but I wasn't able to find any dict-style insertion like in Python. There were a few languages (Java, Tcl) with the N$ positional-style insertion, but I don't think that helps us much.

People have also been discussing a builtin format() function to replace the current % operator. Translating into Python the location of the formatting operations in the languages above suggests the following possibilities:

Of course, these possibilities aren't mutually exclusive, but TOOWTDI suggests that we probably shouldn't have too many of them.

If people know of other languages that have a different approach to string formatting, it might be useful to see them. BTW, I intentionally didn't go into Perl's string formatting because I don't know it that well, and figured there are people on this list much more qualified than myself to present it.

[1]http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html [2]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconFormattingOverview.asp [3]http://www.tcl.tk/man/tcl8.0/TclCmd/format.htm [4]http://gauss.gwydiondylan.org/books/drm/drm_57.html [5]http://caml.inria.fr/pub/docs/manual-ocaml/libref/Printf.html [6]http://www.rubycentral.com/book/ref_c_string.html#String._pc [7]http://www.rubycentral.com/book/ref_m_kernel.html#Kernel.sprintf

Steve

You can wordify anything if you just verb it. --- Bucky Katt, Get Fuzzy



More information about the Python-Dev mailing list