1. Home
  2. Computing & Technology
  3. Python

Beginning Python: Python Encodings

From Al Lukaszewski, for About.com

3 of 7

Python's Way of Defining a Unicode String

To define a string as Unicode, one simply prefixes a 'u' to the opening quotation mark of the assignment. So, for example, to define variable x as holding the Hebrew letter shin, we simply write:

x = u"ש"
The value within quotation marks can be literally anything. The basic structure of the statement remains the same:
y = u"Ù„"
z = u"პ"
One important aspect of working in multiple languages and Unicode is that just because you do not see the letter does not mean that it is not there. If the editor in which you are writing these pieces of code does not support the encoding or does not have a font to reflect the alphabet being used, you will see either blank space, rectangular boxes, or aberrant output that looks a bit like comic strip profanity.

Nonetheless, the assignment is made. But a value saved is not worth very much unless you do something with it. While Python easily handles the variable with the Unicode value, we still need to produce some output. Go to the next page to see how.

Explore Python
About.com Special Features

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

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. Beginning Python
  5. Defining a Unicode String in Python

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

All rights reserved.