Python Chinese string Center/Chinese Center Python/python Chinese character string centered: Hand tearing method easy to use __python

Source: Internet
Author: User

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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.