[Tutor] when string,join won't work

Michael P. Reilly arcege@shore.net
Wed, 3 Jan 2001 12:40:34 -0500 (EST)

> string.join assumes that the sequence is all strings.  What do i do if this is
> not the case.  A friend is trying to make db requests and print the returned
> data.  Not every field is a string, so join won't work.  Suggestions?

Just convert them all to strings and join the resulting list:

>>> class Toast:
...   pass
>>> req = ['spam', 1, 'eggs', Toast()]
>>> req
['spam', 1, 'eggs', <__main__.Toast instance at 80d5f00>]
>>> string.join(map(str, req))
'spam 1 eggs <__main__.Toast instance at 80d5f00>'

I would hope that any data you have in the sequence would have a nice
__str__ or __repr__ method (unlike my deliberately naughty Toast class


| Michael P. Reilly, Release Manager  | Email: arcege@shore.net        |
| Salem, Mass. USA  01970             |                                |