'How to transform a string into a Unicode character
I would like to create a pretty simple code to get multiple string inputs and show as Unicode characters, let's say for example:
2119 01b4 2602 210c 00f8 1f24 (This should show 'Python' with some symbols)
But I keep getting the following exception:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \uXXXX escape
I'm trying to use '\u' to keep it simple, but if there's no other way to do this, I wouldn't bother.
My code:
while True:
string = input()
print(f'\u{string}', end='')
I searched and found something in Swift which is exactly what I want to do in Python, but I didn't quite understand that: Print unicode character from variable (swift).
Solution 1:[1]
The problem is that the unicode escape takes precedence over the f-string format specification. It sees "\u{str" as a 4 character escape sequence. You can split this in to two steps: create the escape and then decode. Since unicode characters can exceed 4 bytes, you may as well go large.
>>> import codecs
>>> string = "2119 01b4 2602 210c 00f8 1f24"
>>> for s in string.split(" "):
... print(codecs.decode(rf"\U{s.zfill(8)}", "unicode-escape"), end="")
...
????ø?
Solution 2:[2]
You can't directly construct \uxxxx escape sequences since that is a language construct, but it is more straightforward to use chr to convert Unicode ordinals to characters. Also int(s,16) will convert a hexadecimal string to an integer:
>>> print(''.join(chr(int(x,16)) for x in input().split()))
2119 01b4 2602 210c 00f8 1f24
????ø?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tdelaney |
| Solution 2 | Mark Tolonen |
