portable unicode literals

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Oct 15 09:26:01 EDT 2012


On Mon, 15 Oct 2012 15:05:01 +0200, Ulrich Eckhardt wrote:

> Hi!
> 
> I need a little nudge in the right direction, as I'm misunderstanding
> something concerning string literals in Python 2 and 3. In Python 2.7,
> b'' and '' are byte strings, while u'' is a unicode literal. In Python
> 3.2, b'' is a byte string and '' is a unicode literal, while u'' is a
> syntax error.
> 
> This actually came as a surprise to me, I assumed that using b'' I could
> portably create a byte string (which is true) and using u'' I could
> portably create a unicode string (which is not true). This feature would
> help porting code between both versions. While this is a state I can
> live with, I wonder what the rationale for this is.

It was a mistake that is corrected in Python 3.3.

You can now use u'' to create Unicode literals in both 2.x and 3.3 or 
better. This is a feature only designed for porting code though: you 
shouldn't use u'' in new code not intended for 2.x.


-- 
Steven



More information about the Python-list mailing list