How to concatenate unicode strings ???

Albert Hopkins marduk at letterboxes.org
Tue Apr 26 12:23:26 EDT 2011


On Tue, 2011-04-26 at 17:58 +0200, Ariel wrote:
> Hi everybody, how could I concatenate unicode strings ??? 
> What I want to do is this:
> 
> unicode('this an example language ') + unicode('español') 
> 
> but I get an:
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
> 11: ordinal not in range(128)
> 
> How could I concatenate unicode strings ???
> 
Your problem isn't with concationation. Your problem is with:


unicode('español')


That is your are passing a non-unicode string to the unicode type and,
it seems the default encoding on your system is ASCII, but "ñ"
is not valid ASCII encoding.

So you can do one of two things:

* Use a unicode literal, e.g. u'español'
* pass whatever encoding you are actually using in your byte string,
  e.g. unicode('español', 'utf8')

If you are writing this in a module and you want to use unicode
literals, you should put something similar at the top of the file:

# -*- encoding: utf-8 -*-

HTH,
-a





More information about the Python-list mailing list