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:
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) .
Assuming that you don't really care about whether the \u
syntax is used, this would look like:
while True:
string = input()
print(chr(int(string, 16)), end='')
If you do in fact care for some reason:
while True:
string = input()
print((br'\u' + string.encode('utf-8')).decode('unicode_escape'), end='')
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="")
...
ℙƴ☂ℌøἤ
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
ℙƴ☂ℌøἤ
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.