So I have a python script that I'd prefer worked on python 3.2 and 2.7 just for convenience.
Is there a way to have unicode literals that work in both? E.g.
#coding: utf-8
whatever = 'שלום'
The above code would require a unicode string in python 2.x (u''
) and in python 3.x that little u
causes a syntax error.
Edit - Since Python 3.3, the
u''
literal works again, so theu()
function isn't needed.The best option is to make a method that creates unicode objects from string objects in Python 2, but leaves the string objects alone in Python 3 (as they are already unicode).
You would then use it like so:
In 3.0, 3.1, and 3.2:
Source: ubershmekel, in the question. See revision 4 for the original.