[Tutor] modulus

Wayne Werner waynejwerner at gmail.com
Wed Nov 16 17:28:40 CET 2011


On Wed, Nov 16, 2011 at 10:09 AM, Steven D'Aprano <steve at pearwood.info>wrote:

> Wayne Werner wrote:
> <snip>
>
>> In old style formatting, you use a string with format specifiers (%s, %d,
>> etc.) followed by a tuple of arguments. Here, the lengths have to match
>> exactly - if you have one specifier then you must have a 1-element tuple.
>>
>
> That's actually wrong. If you have one specifier, you must have one object
> of any sort *except* a tuple.


I think you misunderstood - I said a 1-element tuple - e.g. (3,)


>

>>> "%s" % 42  # One object not a tuple.
> '42'
>
> But if you have a tuple, the % formatting will try to use each element in
> the tuple separately:
>
> >>> "%s" % (23, 42)  # One object which is a tuple
>

As above, that's a two-element tuple. It was explained to me once that in
this case:

"%s" % 42

That since python expects to see a single-element tuple it treats it as or
converts 42 to a single element tuple.

I suppose I could have called it a tuple of length 1 instead.

Sorry for the mistunderstanding,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111116/32e7e8ea/attachment.html>


More information about the Tutor mailing list