string formatting

Neil Cerutti neilc at norwich.edu
Fri May 6 15:54:14 EDT 2011


On 2011-05-06, harrismh777 <harrismh777 at charter.net> wrote:
> Steven D'Aprano wrote:
>> It's perfectly safe to continue using % formatting, if you
>> choose.
>
> I would hope so, since its the way in most of the books, much
> of the doc and a majority of the code...
>
> I don't really like the old style, not because there is
> anything wrong with it, because its an obvious carry-over from
> the cryptic formatting style of 'C' printf(), and others. It
> seems to me that the purpose of formatting is to make output
> clear, and therefore the output formatting style itself should
> be clear and clean.

C's printf is a venerable example of the power of notation.
Notation kicks ass. Another that's well known are regular
expressions. Python uses this powerful idea again in the struct
module. Any other places?

Functions and classes are a general purpose, though verbose, form
of notation and can be used in place of mini languages when you
don't want to bother, e.g., C++'s iostreams, and pyparsing's
grammar declarations.

Lisp declared that you could implement mini-languages in Lisp,
rather than just parsing them.

Python 3's format notation is an improvement for Python, since
Python doesn't need the type information that's crucial for C and
particularly scanf, an application of C's mini-language that
Python doesn't need. Delimiting the escape sequences also makes
it easier to read and parse complex formating declarations.

For simple constructs there's not much difference between them,
but if you switch to .format you'll probably reap some benefit.

-- 
Neil Cerutti



More information about the Python-list mailing list