how does the % work?
Rick Johnson
rantingrickjohnson at gmail.com
Sat Mar 23 03:57:19 EDT 2013
On Saturday, March 23, 2013 2:38:23 AM UTC-5, Steven D'Aprano wrote:
> On Fri, 22 Mar 2013 21:29:48 -0700, Tim Roberts wrote:
> > print('''Ah, so your name is %s, your quest is %s, and your
> > favorite color is %s.''') % (name, quest, color)
>
> The difference between those two statements may not be entirely clear to
> someone not experienced in reading code carefully.
I think the main problem with the code the OP presented is his attempt to stuff a long string literal into the print function. He should have separated the format string from the format operation:
py> fmtstr = '''Ah, so your name is %s, your quest is %s, and your
favorite color is %s.'''
py> print(fmtstr%(1,2,3))
Ah, so your name is 1, your quest is 2, and your
favorite color is 3.
Sorry but i was too lazy to type out three string literal arguments, and instead, I reached for the low hanging syntactical sweetness of the integer peach. Mmm peaches!
More information about the Python-list
mailing list