Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > portable unicode literals

Reply
Thread Tools

portable unicode literals

 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      10-15-2012
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.

!puzzled thanks

Uli
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-15-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
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      10-15-2012
On 10/15/2012 09:05 AM, 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.
>
> !puzzled thanks
>
> Uli


Python 3.3 added that syntax, for easier porting. You can now use
u"xyz" for a unicode string in both 2.x and 3.3

--

DaveA

 
Reply With Quote
 
Alex Strickland
Guest
Posts: n/a
 
      10-15-2012
On 2012/10/15 03:05 PM, Ulrich Eckhardt wrote:

> 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.
>
> !puzzled thanks


u'' is legal in 3.3 again.

--
Regards
Alex
 
Reply With Quote
 
Serhiy Storchaka
Guest
Posts: n/a
 
      10-15-2012
On 15.10.12 16:05, Ulrich Eckhardt wrote:
> 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.


from __future__ import unicode_literals

And now you can portable use b'' for a byte string and '' for a unicode
string. When you will drop Python 2 support then just remove import from
__future__.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unicode literals to latin-1 Python 6 01-30-2008 02:47 PM
Making all string literals Unicode Richie Hindle Python 0 05-15-2006 08:57 AM
Java: byte literals and short literals John Goche Java 8 01-17-2006 11:12 PM
Unicode String Literals didster@gmail.com Java 4 11-21-2005 11:43 AM
unicode string literals and "u" prefix nico Python 6 11-10-2004 09:14 AM



Advertisments