如何 "print a UTF8 string"
How to "print a UTF8 string"
我有一个字符串:
"123456789012"
这样打印出来可以吗?
print("\u1234\u5678\u9012")
使用功能?前任。 print_utf8(string)
# Split the string into chunks of length 4
In [1]: codepoints = ["1234", "5678", "9012"]
# Convert them into the `\u` format
In [2]: r'\u' + r'\u'.join(codepoints)
Out[2]: '\u1234\u5678\u9012'
# Decode
In [3]: _.encode().decode('unicode-escape')
Out[3]: 'ሴ噸递'
请注意,在 Python 3 中,字符串已采用 Unicode。这就是为什么您需要 .encode()
使用 Unicode 转义的字符串,然后 .decode()
它。参见
我有一个字符串:
"123456789012"
这样打印出来可以吗?
print("\u1234\u5678\u9012")
使用功能?前任。 print_utf8(string)
# Split the string into chunks of length 4
In [1]: codepoints = ["1234", "5678", "9012"]
# Convert them into the `\u` format
In [2]: r'\u' + r'\u'.join(codepoints)
Out[2]: '\u1234\u5678\u9012'
# Decode
In [3]: _.encode().decode('unicode-escape')
Out[3]: 'ሴ噸递'
请注意,在 Python 3 中,字符串已采用 Unicode。这就是为什么您需要 .encode()
使用 Unicode 转义的字符串,然后 .decode()
它。参见