str(list)

Fredrik Lundh fredrik at pythonware.com
Wed Jun 9 17:10:55 EDT 2004


Trevor Blackwell wrote:

> I wish that str of a list would call str on the elements, rather than
> repr. Currently, list has only a repr function so it ends up calling
> repr on its members

this is by design, of course: the list type is a container type, and has no
"natural" string representation.  (given a list ["x", "y"], is "[x, y]" really
more correct than, say, "x, y"?  or "x y"?  or "xy"?  or "x\ny"?)

if you want to convert the members of a container to strings and combine
them in given way, you should spell it out.

if you do it a lot, write a one-line function that renders any sequence (this is
Python, after all) in your favourite format.

</F>







More information about the Python-list mailing list