Join strings - very simple Q.

Diez B. Roggisch deets at nospam.web.de
Sat Mar 24 19:07:56 CET 2007


7stud schrieb:
> On Mar 24, 8:30 am, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
>> In case you are feeling that the ','.join(l) looks a bit jarring, be aware
>> that there are alternative ways to write it. You can call the method on the
>> class rather than the instance:
>>
>>    jl = str.join(',', l)
>>    jl = unicode.join(u'\u00d7', 'l')
>>
>> ... the catch is you need to know
>> the type of the separator in advance.
> 
> 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 "test1.py", 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".

Diez



More information about the Python-list mailing list