What does the 'u' symbol mean in front of string values?

2 views
Skip to first unread message

jdi via StackOverflow

unread,
Aug 14, 2014, 12:46:12 PM8/14/14
to google-appengin...@googlegroups.com

The 'u' in front of the string values means the string has been represented as unicode. It is a way to represent more characters than normal ascii can manage.

You can convert a string to unicode multiple ways:

>>> u'foo'
u'foo'
>>> unicode('foo')
u'foo'

But the real reason is to represent something like this (translation here):

>>> val = u'Ознакомьтесь с документацией'
>>> val
u'\u041e\u0437\u043d\u0430\u043a\u043e\u043c\u044c\u0442\u0435\u0441\u044c \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439'
>>> print val
Ознакомьтесь с документацией

For the most part, you shouldn't have any errors in treating them different than ascii strings in this code.

There are other symbols you will see, such as the "raw" symbol for telling a string not to interpret any special characters. This is extremely useful when doing regular expression in python.

>>> 'foo\"'
'foo"'
>>> r'foo\"'
'foo\\"'

ACSII and Unicode strings can be logically equivalent:

>>> bird1 = unicode('unladen swallow')
>>> bird2 = 'unladen swallow'
>>> bird1 == bird2
True


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/11279331/what-does-the-u-symbol-mean-in-front-of-string-values/11279428#11279428
Reply all
Reply to author
Forward
0 new messages