list to string

Michael Gilfix mgilfix at eecs.tufts.edu
Tue Jul 23 17:40:06 EDT 2002


  It sounds like you want to do something like this:

list = [ 'this', ' ', 'is', ' a', ' test' ]
string = ''.join (list)

  Or if you want a space and you just have a list
of strings:

string = ' '.join (list)

  The character in the source string is used to join the elements in
the list of the join() call.  So in the context of what you're written
here, you could do:

  file = open ('foo.txt', 'r')
  lines = file.readlines ()
  all_text = ''.join (lines)

                 -- Mike

On Tue, Jul 23 @ 13:08, David LeBlanc wrote:
> I have this code:
> 
> fn = file('foo.txt')
> 
> txt = fn.readlines()
> 
> str = ?something?(txt) # or txt.?something?()...
> 
> What's the something? This is probably so obvious, i'm going to feel dumb,
> but I havn't been able to find anything in any doc I've looked at.
> 
> David LeBlanc
> Seattle, WA USA
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
`-> (whisper)

-- 
Michael Gilfix
mgilfix at eecs.tufts.edu

For my gpg public key:
http://www.eecs.tufts.edu/~mgilfix/contact.html




More information about the Python-list mailing list