converting an array of chars to a string

Gerhard Häring gerhard.haering at gmx.de
Fri Jun 21 10:32:15 CEST 2002


* Derek Thomson <derek at wedgetail.com> [2002-06-21 08:22 +0000]:
> JB wrote:
> >How can I convert an array of chars to string?
> >
> >The problem is this: I should like to perform 8 bit 
> >calculations in a buffer. After this has been done, the 
> >buffer has to ba written to a binary file (and file.write 
> >takes strings instead of lists).
> >
> 
> Easy, just use the "join" function to concatenate a sequence of strings 
> into a single string. For example:
> 
> 
> import string
> 
> list = ['a', 'b', 'c', 'd']
> 
> s = string.join(list, '')
> 
> print s

Unless Python 1.5.2 compatibilty is a requirement, just use string
methods:

    lst = ['a', 'b', 'c', 'd']
    s = ''.join(lst)

It's also not a good idea to replace the builtin list function, thus I'm
using 'lst' here.

Gerhard
-- 
This sig powered by Python!
Außentemperatur in München: 25.1 °C      Wind: 3.7 m/s





More information about the Python-list mailing list