Chinese characters occupy the equivalent of two words in the character, but the font design, the general one Chinese character width does not equal to the width of two English characters, so the effect of printing is biased.
Such as:
c = [' Summary ', ' summary ', ' summary ', ' summary ', ' summary ', ' summary ', ' summary ', ' decision ', ' the ' determined '
]
print ('---- Normal string format:----') for
x in range (len (c)):
print (' |%20s| '% c[x])Print the results as shown in the following illustration:
However, the width of the equal-width character is exactly equal to the width of two English characters in a Chinese font. We can calculate the length of the string automatically according to this.
def Chinese (data):
count = 0 for
s in data:
if Ord (s) > 127:
count + 1 return
count
prin T ('----format by function calculation length:----') for
x in range (len (c)): Number
= Chinese (c[x])
newstr = ' {0:{wd}} '. Format (c[ X],wd=20-number)
print (' |%s| '% newstr)
github:https://github.com/jueee/04-liaoxuefeng/blob/master/81-chinese.py