1

If I define the variable

x = 'Ááa Éée'

then the output of

print x

is

Ááa Éée

But I have an unicode object

x = u'Ááa Éée'

and I need the same output as before. To do this, I tried converting it to a str with

str(u'Ááa Éée')

but it didn't work.

How can I do this? (I'm only interested exit.)

0

3 Answers 3

4

Actually, print u"Ááa Éée" should give you the exact same output as print "Ááa Éée". Maybe you are confusing with printing the representation of each one on the terminal. Anyway, if what you're asking is how to convert the unicode to str, use x.encode('utf-8').

Sign up to request clarification or add additional context in comments.

Comments

2

str(u'Ááa Éée') is not working, because this conversion unicode -> str uses encoding ASCII by default and characters ÁáÉé are not present in ASCII.

You need this: u'Ááa Éée'.encode("UTF-8") - if your terminal uses UTF-8.

Things about unicode can be complicated, it's better to read something about it:

Comments

0

x = u'Ááa Éée' is unicode string

so, you You can only use unicode() instead str()

use unicode string

You need to specify the encoding

string = x.encode('utf-8') #utf-16 or ...

print string.decode('utf-8')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.