Why do this?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Oct 5 06:52:13 EDT 2006


In <mailman.1336.1160044062.10491.python-list at python.org>, Matthew Warren
wrote:

> I learned over the years to do things like the following, and I like
> doing it like this because of readability, something Python seems to
> focus on :-
> 
> Print "There are "+number+" ways to skin a "+furryanimal
> 
> But nowadays, I see things like this all over the place;
> 
> print("There are %s ways to skin a %s" % (number, furryanimal))
> 
> Now I understand there can be additional formatting benefits when
> dealing with numbers, decimal places etc.. But to me, for strings, the
> second case is much harder to read than the first.

For me it's the other way around -- I find the second one more readable
especially without syntax highlighting to see which characters are inside
and which are outside the quotes.

With the second one there's a clear visual separation of the string
literal and the variables, even without colors.

Another advantage of the second way is much easier localization and other
tasks where the sentence itself is not hardcoded but read from files,
databases, etc.

And the '%s' automatically converts the objects to strings.  The
equivalent to the second example without string formatting would be::

  print 'There are' + str(number) + ' ways to skin a ' + str(furryanimal)

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list