Skip to content Skip to sidebar Skip to footer

Python Dictionary Key/value With Prefixes - What's The Prefix For?

I've seen a Python dict looks like this lately: test1 = {u'user':u'user1', u'user_name':u'alice'} This confuses me a bit, what is the u before the key/value pair for? Is it some s

Solution 1:

In Python 2, you have to force Unicode character to remain in Unicode.

So, u prevents the text to translate to ASCII. (remaining as unicode)

For example, this won't work in Python 2:

'ô SO'.upper() == 'Ô SO''

Unless you do this:

u'ô SO'.upper() == 'Ô SO'

You can read more on this: DOCS

Some history: PEP 3120


Solution 2:

u'unicode string' will make the string a type unicode, where without the prefix the string is an ASCII type string 'ASCII string'.


Post a Comment for "Python Dictionary Key/value With Prefixes - What's The Prefix For?"