1

I am playing around working with unicode in python. I am not able to print (display) the unicode characters such asé I tried the following:

>>> sys.setdefaultencoding('UTF8')
>>> chr(0xFF)
'\xff'
>>> u = u'abcdé'
>>> len(u)
5
>>> u[4]  
u'\xe9'
>>> str(u[4])
'\xc3\xa9'
>>>     

I was expecting u[4] to print é but it prints u'\xe9'. how can I make this work? I am using python 2.7.2 version

1 Answer 1

6

When you just type u[4], it shows the repr. To see the unicode character, use print.

print u[4]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! that worked, so setting sys.setdefaultencoding('UTF8') is not needed I guess?
@eagertoLearn: No, don't use setdefaultencoding(); that is very rarely a good idea.
@MartijnPieters: Thanks, I will not use it. but may I know why is it a bad idea? and whats it purpose then?
@eagertoLearn: It's an internal method used by Python to set the default encoding from the environment, to match your terminal settings. Mostly, people setting it don't understand how automatic encoding and decoding works for Unicode values, and should instead investigate how to avoid the automatic conversions.

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.