[Tutor] Converting string or list to sum

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Jan 30 05:29:55 EST 2004



On Fri, 30 Jan 2004, Alan Gauld wrote:

> >  I think I can put two ideas together to solve it for lists :
> >  >>> a=[2, 3, '+', 4]
> >  >>> d=""
> >  >>> for item in range(len(a)):
> >  ...     d=d+str(a[item])
> >  ...
>
> Or even easier:
>
> for item in a:
>     d = d + str(item)
>
> No need for indexing etc,


By the way, strings also support an explicit 'join()' method that can take
a list as an argument:

    http://www.python.org/doc/lib/string-methods.html#l2h-188

So there's actually no need for the loop either.  *grin*


Talk to you later!




More information about the Tutor mailing list