I have this string \\u2605
which codes for ★
, and I want to change it so that:
It prints either ★
or the "?" command prompt replacement, rather than \\u2605
I can send the string into functions as ★
not \\u2605
How do I change/encode the string?
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' )