1. Home
  2. Computing & Technology
  3. Python

Beginning Python: Python Encodings

From , former About.com Guide

6 of 7

Encoding ASCII as Unicode with Python

But what if we want to encode a standard string as Unicode? While there are some who think that, when it is spoken slowly enough and loudly enough, everyone understands English. This method of communication works even less for computers than it does for humans.

As mentioned earlier, Python's default encoding is usually ASCII. It can, however, be set to other encodings. To find out which encoding a given Python installation is using, use the method sys.getdefaultencoding(). In the Python shell, it will look like this:

>>> import sys >>> sys.getdefaultencoding() 'ascii'
To convert a standard, ASCII string to Unicode, we simply use Python's built-in unicode function:
t = "Hello"
x = unicode(t)
Now, x contains the Unicode form of t. You might ask: What's the difference? On your local system, probably none. If, however, you are a consultant programming in Missoula, Montana for a Greek-speaking client in Athens, there is a substantial difference. See the next page for a more detailed explanation.

Explore Python
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. Beginning Python
  5. Encoding ASCII as Unicode with Python

©2009 About.com, a part of The New York Times Company.

All rights reserved.