There is a problem in making a small format output: No matter how the row, Chinese characters can not be very smoothly centered, especially Chinese characters and English or digital together, is a mess.
Python has a built-in string method, Str.center (x), that lets the string output and center in the length of I.
But.
But.
But.
The default is the length of ASCII when calculating the length of the string, that is, a character length of 1.
This obviously doesn't work because one Chinese occupies 2 of a length. The same is the Chinese symbol.
So if you want the x= ' centered Juzhong ' string to be centered, you can't do it with Str.center (Len (x)), because Len (x) =9 rather than actually 11, the result of the output that's naturally pretty bad. So I wrote a function that could read the correct string length:
def LENCN (n):
#遇到非英文, automatically twice times the length.
n=list (n)
stl=0 for
x in N:
if x in string.printable:
STL +=1
Else:
stl+=2 return
STL
This allows the correct length of the string to be read, but it is not enough for a smooth center layout. The right thing to do is to use this function to read the actual length of all units (such as ' names '), to find the maximum, (such as ' Aixinjueluo dog eggs ', 12), plus some empty lengths for aesthetic consideration (at random, like 4). Then let all the name units output with print (X.center (12+4)).
Sometimes, such output is still not able to center (odd even-numbered spaces problem).
This time, you can use the small function I wrote to complete.
def Merg (x,y):
a=int ((Maximum length list [project name]-CHECKCN (current item))/2 B=int ((
(Maximum length list [project name]-CHECKCN (current item))/2)
if A+b!= (( Maximum length list [project name]-CHECKCN (current item)):
b+=1 #防止出现奇数项的情况 return
(A,b,)
def Getmerg (x,y):
y= ' *merg (x,y) [0]+y+ ' *merg (x,y) [1] return
y
Must be able to achieve the goal.