String to unicode - duplicating by function the effect of u prefix

Discussion in 'Python' started by CiTro, Jun 18, 2009.

  1. CiTro

    CiTro Guest

    I'm looking for a way to convert a string to it's unicode "brother".

    This means:

    stringOne = "\u0026"
    stringTwo = u"\u0026"

    print unicode(stringOne) == stringTwo

    The result is false. What function should I use, to duplicate the
    effect of the "u" prefix ?
     
    CiTro, Jun 18, 2009
    #1
    1. Advertising

  2. CiTro

    Peter Otten Guest

    CiTro wrote:

    > I'm looking for a way to convert a string to it's unicode "brother".
    >
    > This means:
    >
    > stringOne = "\u0026"
    > stringTwo = u"\u0026"
    >
    > print unicode(stringOne) == stringTwo
    >
    > The result is false. What function should I use, to duplicate the
    > effect of the "u" prefix ?


    "\u..." has a special meaning only in unicode, not string literals:

    >>> s = "\u0026"
    >>> len(s)

    6
    >>> s.decode("unicode-escape")

    u'&'
    >>> _ == u"\u0026"

    True

    Peter
     
    Peter Otten, Jun 18, 2009
    #2
    1. Advertising

  3. CiTro

    CiTro Guest

    Re: String to unicode - duplicating by function the effect of uprefix

    Thank you, Peter. That solved my problem.
     
    CiTro, Jun 18, 2009
    #3
  4. CiTro

    Vincent Guest

    Re: String to unicode - duplicating by function the effect of uprefix

    On Jun 18, 3:23 pm, CiTro <> wrote:
    > Thank you, Peter. That solved my problem.


    the another way is,

    codecs.raw_unicode_escape_decode(stringOne) == stringTwo
     
    Vincent, Jun 18, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Tyler
    Replies:
    36
    Views:
    1,467
    Darryl L. Pierce
    Dec 10, 2004
  2. nico
    Replies:
    6
    Views:
    783
  3. Chris  Chiasson
    Replies:
    6
    Views:
    622
    Richard Tobin
    Nov 14, 2006
  4. Adam Akhtar

    duplicating characters in a string

    Adam Akhtar, Mar 6, 2008, in forum: Ruby
    Replies:
    29
    Views:
    246
    Paul Brannan
    Mar 12, 2008
  5. Duplicating a string?

    , Jun 17, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    89
    Prasanna
    Jun 17, 2005
Loading...

Share This Page