[Tutor] String to List and back?

Chuck Allison chuck at freshsources.com
Sun Jul 3 06:06:28 CEST 2005

Hello Chinook,

How about join(), as in



Saturday, July 2, 2005, 9:45:28 PM, you wrote:

C> I'm missing something simple again.  The simplified issue is:

C> Python 2.4.1 (#2, Mar 31 2005, 00:05:10)

C>  >>> mystr = 'abc'

C>   # I can create a list of the string characters
C>   #  with list comprehension
C>  >>> [c for c in mystr]
C> ['a', 'b', 'c']

C>   # Or just a simple builtin conversion function
C>  >>> list(mystr)
C> ['a', 'b', 'c']

C>   # But a conversion back to a string simply makes the
C>   #  the list a string (I know there would have to be
C>   #  special handling, but I noted it for illustration)
C>  >>> str(['a', 'b', 'c'])
C> "['a', 'b', 'c']"

C>   # I know I could get the original string back by
C>   #  rebuilding it with "for" loop
C>  >>> bts = ''
C>  >>> for c in strlist:
C> ...   bts += c
C> ...
C>  >>> bts
C> 'abc'

C>   # or even a function or class method which implemented
C>   #  the "for" loop
C>   # I could also build a new string directly from the
C>   #  original with a "for" loop

C> Is there a one-liner like a builtin conversion function or list 
C> comprehension technique for the reverse (i.e. a list of strings back to 
C> a single string)?

C> Thank you,

C> Lee C

C> _______________________________________________
C> Tutor maillist  -  Tutor at python.org
C> http://mail.python.org/mailman/listinfo/tutor

Best regards,

More information about the Tutor mailing list