1

I am using python format method to align East Asian Wide (EAW) characters in my terminal display. The following python3 code illustrates my problem.

ZH_STRING1 = "東京大学"
ZH_STRING2 = "麻将"

print(">" + "01234567" + "<")
print(">" + format(ZH_STRING1, ">4") + "<")
print(">" + format(ZH_STRING2, ">4") + "<")
print("---")
print("Length:         ", len(ZH_STRING2))

The terminal output (see image below) shows that EAW character width is twice the width of an ordinary character, while the len function return the correct number of characters (one for each ideograph).

Terminal output of bad east asian wide characters aligment.

The python format function evaluate ZH_STRING1 length as 4 and ZH_STRING2 length as 2, and the format function adds two space characters to make the alignment. Unfortunately, the different width between EAW and space characters messes the terminal output.

So, how can I het the correct aligment?

jorispilot
  • 483
  • 2
  • 6

1 Answers1

2

In fact, I found a solution while writing this question. ^^

It is quite simple: Use the Unicode Ideographic Space (U+3000) in the python format function, which takes to normal spaces on the terminal output:

ZH_STRING1 = "東京大学"
ZH_STRING2 = "麻将"

print(">" + "01234567" + "<")
print(">" + format(ZH_STRING1, " >4") + "<")
print(">" + format(ZH_STRING2, " >4") + "<")

Then:

enter image description here

jorispilot
  • 483
  • 2
  • 6