list to string

Brian Quinlan BrianQ at ActiveState.com
Tue Feb 27 19:14:33 EST 2001


Nice work. That is a pretty complex construction for someone new. But there
are other ways to get this to work that are a bit cleaner:

import string

mylist = ['d','o','g']
mystring = string.join( mylist, '' )
print mystring

OR (and I hate this style):

mylist = ['d','o','g']
mystring = ''.join( mylist )
print mystring

OR (if you want to do the loop yourself):

mylist = ['d','o','g']
mystring = ''
for j in mylist:
    mystring = mystring + j
print mystring

-----Original Message-----
From: python-list-admin at python.org
[mailto:python-list-admin at python.org]On Behalf Of Glen Mettler
Sent: Tuesday, February 27, 2001 2:54 PM
To: python-list at python.org
Subject: Re: list to string


I couldn't get "for element in mylist" to work but this does:

mylist = ['d','o','g']
mystring = ''
for j in range(len(mylist)):
    mystring= mystring+"".join(mylist[j])
print mystring

I am very, very new to Python so this may not be very elegant but it does
work

Glen  (I asked the original question)
Thanks for all help!





More information about the Python-list mailing list