Join strings - very simple Q.

Duncan Booth duncan.booth at invalid.invalid
Sat Mar 24 19:20:04 CET 2007

"Diez B. Roggisch" <deets at> wrote:

> 7stud schrieb:
>> When I try the latter example, I get an error:
>> lst = ["hello", "world"]
>> print unicode.join(u"\u00d7", lst)
>> Traceback (most recent call last):
>>   File "", line 2, in ?
>>     print unicode.join(u"\u00d7", lst)
>> UnicodeEncodeError: 'ascii' codec can't encode character u'\xd7' in
>> position 5: ordinal not in range(128)
> You are mixing unicode with bytestrings here. make "hello" u"hello", 
> same for "world".
Sorry Diez, wrong answer. A unicode separator will cause all the strings 
being joined to be decoded using the default encoding (which could cause 
problems with non-ascii characters in the decoded strings), but the problem 
here is with encoding, not decoding.

7stud: the problem isn't the join, it is printing the string on your 
terminal which is the problem. Try just:

   print u"\u00d7"

and you'll get the same problem. Or:

 lst = ["hello", "world"]
 joined = unicode.join(u"\u00d7", lst)

will work, but you'll still have problems printing the result.

If you try it using a Python interpreter with an appropriate output 
encoding it will work (idle can handle it on my system, but the DOS prompt 
with its default codepage cannot).

More information about the Python-list mailing list