Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8969649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:37:21+00:00 2026-06-15T17:37:21+00:00

I have a string in python 3 that has several unicode representations in it,

  • 0

I have a string in python 3 that has several unicode representations in it, for example:

t = 'R\\u00f3is\\u00edn'

and I want to convert t so that it has the proper representation when I print it, ie:

>>> print(t)
Róisín

However I just get the original string back. I’ve tried re.sub and some others, but I can’t seem to find a way that will change these characters without having to iterate over each one.
What would be the easiest way to do so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:37:22+00:00Added an answer on June 15, 2026 at 5:37 pm

    You want to use the built-in codec unicode_escape.

    If t is already a bytes (an 8-bit string), it’s as simple as this:

    >>> print(t.decode('unicode_escape'))
    Róisín
    

    If t has already been decoded to Unicode, you can to encode it back to a bytes and then decode it this way. If you’re sure that all of your Unicode characters have been escaped, it actually doesn’t matter what codec you use to do the encode. Otherwise, you could try to get your original byte string back, but it’s simpler, and probably safer, to just force any non-encoded characters to get encoded, and then they’ll get decoded along with the already-encoded ones:

    >>> print(t.encode('unicode_escape').decode('unicode_escape')
    Róisín
    

    In case you want to know how to do this kind of thing with regular expressions in the future, note that sub lets you pass a function instead of a pattern for the repl. And you can convert any hex string into an integer by calling int(hexstring, 16), and any integer into the corresponding Unicode character with chr (note that this is the one bit that’s different in Python 2—you need unichr instead). So:

    >>> re.sub(r'(\\u[0-9A-Fa-f]+)', lambda matchobj: chr(int(matchobj.group(0)[2:], 16)), t)
    Róisín
    

    Or, making it a bit more clear:

    >>> def unescapematch(matchobj):
    ...     escapesequence = matchobj.group(0)
    ...     digits = escapesequence[2:]
    ...     ordinal = int(digits, 16)
    ...     char = chr(ordinal)
    ...     return char
    >>> re.sub(r'(\\u[0-9A-Fa-f]+)', unescapematch, t)
    Róisín
    

    The unicode_escape codec actually handles \U, \x, \X, octal (\066), and special-character (\n) sequences as well as just \u, and it implements the proper rules for reading only the appropriate max number of digits (4 for \u, 8 for \U, etc., so r'\\u22222' decodes to '∢2' rather than ''), and probably more things I haven’t thought of. But this should give you the idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that has several date values in it, and I want
I have some code in a python string that contains extraneous empty lines. I
i have string stored in python variables, and i am outputting a html that
I have a Python regex that takes a string (database connection URI) and splits
Possible Duplicate: python dict.add_by_value(dict_2) ? My input is two dictionaries that have string keys
I have a function in my Django app that has a dictionary containing several
I have a function that has several outputs, all of which native, i.e. integers
My doc strings have references to other python classes that I've defined. Every time
I have a string in Python 2.7.2 say u\u0638. When I write it to
I have a string in the format: 'nn.nnnnn' in Python, and I'd like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.