1. Computing & Technology

Beginning Python: Python Encodings

From , former About.com Guide

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.

©2012 About.com. All rights reserved.

A part of The New York Times Company.