how does the % work?

Michael Torrie torriem at gmail.com
Sat Mar 23 11:57:48 EDT 2013


On 03/23/2013 01:38 AM, Steven D'Aprano wrote:
> Just to add confusion, the two lines are exactly the same in Python 2, 
> where Print is not a function! 

Perhaps this is a good reason use the slightly more complicated but
easier to get right format method.

print ("{0}, {1}, {2}".format(1,2,3))

And it's worth noting that using the string formatter or the old %
operator on a string works anywhere where there're strings, not just in
the print function.

b = "The answer was %d" % answer

or

b = "The answer was {0}".format(answer)

On the topic of print, I can see the logic in print being a function
now, but I have always preferred it as a print statement; it just looked
cleaner.  That was one of the things that initially attracted me to
python.  But the reality is print isn't used for much in real programs
other than console debug messages.



More information about the Python-list mailing list