Join strings - very simple Q.

Max Erickson maxerickson at gmail.com
Sat Mar 24 14:24:15 EDT 2007


"7stud" <bbxx789_05ss at yahoo.com> wrote:

> 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)
> 

Your terminal is likely the problem. Get rid of the print:

q=unicode.join(u'\u00d7',['hello','world'])

and you will probably get rid of the exception.

(so I guess the issue is the display, not the logic)


max




More information about the Python-list mailing list