tuple to string/list

David C. Fox davidcfox at post.harvard.edu
Thu Aug 21 16:02:48 EDT 2003


WIWA wrote:
> Hi,
> 
> I'm a newbie learning to appreciate the strength of Python. I'm
> writing an application to look at the access_log of my apache server:
> 
> When I write:
> 
> for i in range(len(month)):
>     output="Hits for",month[i], ":" , teller[i]
>     f.write(str(output))
> f.close()
> 
> => this produces a tuple:
> ('Hits for', 'Jan', ':', 0)('Hits for', 'Feb', ':', 2)('Hits for',
> 'Mar', ':', 3)
> 
> => whereas I would like the following output:
> Hits for Jan: 0
> Hits for Feb: 2
> Hits for Mar: 3
> 
> Any easy way of obtaing this output?

     output = "Hits for %s: %d\n" % (month[i], teller[i])

or

     output = "Hits for " + month[i] + ": " + str(teller[i]) + "\n"

and then

     f.write(output)

See 7.1 in the Tutorial

David





More information about the Python-list mailing list