pythonunicodeprintingfonts

How to get unicode characters to print correctly using python?


I'm using python to print certain characters using Unicode code but I ran into a problem. I can only print certain characters. For example:

lista = ['\u23fb', '\u23fc', '\u23fd', '\u23fe', '\ue001', '\ue002',' \ue003', '\ue004' ,'\ue005']

print(lista)

will give me as a result:

['⏻', '⏼', '⏽', '⏾', '\ue001', '\ue002', ' \ue003', '\ue004', '\ue005']

Also if I loop through the list and print each item, it won't work,

but if I use:

print(*lista)

it will print correctly:

correctly printed chars

So why is there a difference?

It also works if I print each one with a print argument for example:

print("\ue001")

will result in the correct output.

I've tried different formatting methods with no luck.


Solution

  • When a list is printed, Python used the str.__repr__ function to print the items in the list. print uses the str.__str__ function. The former is a programmer-friendly version that shows unprintable or undefined Unicode characters as escape codes. U+E000 - U+F8FF is a private use area which is considered undefined. print will display whatever character your font has for that location or an undefined symbol (U+FFFD, �, varies by font) if undefined in the font.