Difference in formatting list and tuple values
Corrado Gioannini
gioco at nekhem.com
Thu Dec 13 12:08:28 EST 2001
that's because the string formatting syntax requires a tuple (and not a
list) as the argument after the "%".
t[:1] is a tuple, l[:1] is a list.
corrado.
On Thu, Dec 13, 2001 at 10:00:56AM -0700, Sue Giller wrote:
> I am trying to format values from either a list or a tuple, and I am
> getting the following odd (to me) behavior. I can format the tuple
> entry using %d, but I get an error when I use the same formatting
> for the same data in a list.
> Why does this happen?
>
> >>> l = [1,2,3] # list
> >>> t = (1,2,3) # tuple
> >>> print "%02d" % t[:1] # format first entry in tuple ok
> 01
> >>> print "%02d" % l[:1] # get error with list
> Traceback (most recent call last):
> File "<interactive input>", line 1, in ?
> TypeError: an integer is required
> >>> print "%02d" % tuple(t[:1]) # cast to a tuple works ok
> 01
>
> --
> http://mail.python.org/mailman/listinfo/python-list
--
Corrado Gioannini
<gioco at nekhem.com>
"Thought is only a flash between two long nights,
but this flash is everything."
(H. Poincaré)
More information about the Python-list
mailing list