Change unicode to actual character?

Question:

I have this string \u2605 which codes for , and I want to change it so that:

  1. It prints either or the “?” command prompt replacement, rather than \u2605

  2. I can send the string into functions as not \u2605

How do I change/encode the string?

Asked By: Andrew

||

Answers:

You can pass the bytes to decode('unicode-escape'):

s = '\u2605 Amazing \u2605'

s = bytes(s, 'utf8').decode( 'unicode-escape' )

print(s)
# ★ Amazing ★

note: not sure if this is a problem in your case, but decode( 'unicode-escape' ) does not handle un-escaped, non-ascii, so something like s = '\u2605 Ama ★ zing \u2605' would cause problems. See issue here. A possible solution in this case is to escape the entire string and then decode:

s = s.encode('ascii','backslashreplace').decode( 'unicode-escape' )
Answered By: Mark

For all of those who have this issue in Pycharm terminal or run window, change in settings: Editor -> General -> Console -> Default Encoding to UTF-8

Answered By: Kamil Kostrubiec
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.