Getting unicode escape sequence from unicode character?

Discussion in 'Python' started by Kenneth McDonald, Dec 27, 2006.

  1. Given a Python unicode character (string of length one), how would I
    find out the \uXXXX escape sequence for it? This isn't obvious from the
    docs I've been looking through.

    Thanks,
    Ken
    Kenneth McDonald, Dec 27, 2006
    #1
    1. Advertising

  2. Kenneth McDonald

    Carl Banks Guest

    Kenneth McDonald wrote:
    > Given a Python unicode character (string of length one), how would I
    > find out the \uXXXX escape sequence for it? This isn't obvious from the
    > docs I've been looking through.


    You can use the ord builtin, or the encode method with
    "unicode_escape":

    >>> a = u'\u1234'
    >>> a

    u'\u1234'
    >>> print a

    ሴ
    >>> ord(a)

    4660
    >>> hex(ord(a))

    '0x1234'
    >>> a.encode('unicode_escape')

    '\\u1234'


    Carl Banks
    Carl Banks, Dec 27, 2006
    #2
    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. raavi
    Replies:
    2
    Views:
    901
    raavi
    Mar 2, 2006
  2. slomo
    Replies:
    5
    Views:
    1,506
    Duncan Booth
    Dec 2, 2007
  3. Jeremy
    Replies:
    1
    Views:
    795
    Alex Willmer
    Jan 11, 2011
  4. VUNETdotUS
    Replies:
    3
    Views:
    627
    VUNETdotUS
    Nov 2, 2007
  5. nvangogh
    Replies:
    10
    Views:
    139
    Randy Westlund
    Mar 13, 2014
Loading...

Share This Page