Question about tuple lengths

Jean-Paul Calderone exarkun at divmod.com
Wed Dec 14 18:58:16 CET 2005


On Wed, 14 Dec 2005 09:54:31 -0800, "Carl J. Van Arsdall" <cvanarsdall at mvista.com> wrote:
>
> From my interpreter prompt:
>
> >>> tuple = ("blah")
> >>> len(tuple)
>4
> >>> tuple2 = ("blah",)
> >>> len (tuple2)
>1
>
>So why is a tuple containing the string "blah" without the comma of
>length four? Is there a good reason for this or is this a bug?

It's not a tuple :)

    >>> t = ("blah")
    >>> type(t)
    <type 'str'>
    >>> t2 = ("blah",)
    >>> type(t2)
    <type 'tuple'>
    >>> t3 = "blah",
    >>> type(t3)
    <type 'tuple'>
    >>> 

It's the comma that makes it a tuple.  The parenthesis are only required in cases where the expression might mean something else without them.

Jean-Paul



More information about the Python-list mailing list