Unicode (text) string literals¶

If you are upgrading an existing Python 2 codebase, it may be preferable to mark up all string literals as unicode explicitly with u prefixes:

# Python 2 only s1 = 'The Zen of Python' s2 = u'きたないのよりきれいな方がいい

' # Python 2 and 3 s1 = u'The Zen of Python' s2 = u'きたないのよりきれいな方がいい

'

The futurize and python-modernize tools do not currently offer an option to do this automatically.

If you are writing code for a new project or new codebase, you can use this idiom to make all string literals in a module unicode strings:

# Python 2 and 3 from __future__ import unicode_literals # at top of module s1 = 'The Zen of Python' s2 = 'きたないのよりきれいな方がいい

'

See http://python-future.org/unicode_literals.html for more discussion on which style to use.