[Tutor] Why is this write defined as a tuple, instead of picking list?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Apr 16 14:27:48 EDT 2004



On Fri, 16 Apr 2004, Adam wrote:

> def prepare_for_saving(article_items):
>     """This function formats the article_items list
> so that it's in HTML format and looks nice"""
>     print "article_items is type: ", type(article_items)
>     for x in article_items:
>         write = "<h3>", article_items[0], "</h3>"
>         print "write is type: ", type(write)
>     #write = str(article_items)#deprecated - replaced by the
> formatting loop    return (write)
>
> when I try to do the formatting with write =, write is
> declared as a tuple - why is this, and how can I force it to
> be a string


Hi Adam,


Let's take a closer look at how 'write' is being defined:

    write = "<h3>", article_items[0], "</h3>"

This is defining a tuple of three elements

    ('<h3>', article_items[0], '</h3>')

and that's why we're getting a tuple.


What you probably want, instead, is a concatenation of the three strings.
One way to do concatenation is by using the '+' operator:

    write = '<h3>' + article_items[0] + '</h3>'

By doing this, 'write' should have all three strings glued together.



See:

    http://www.python.org/doc/tut/node5.html#SECTION005120000000000000000

for more information on string manipulation.


Hope this helps!




More information about the Tutor mailing list