奇妙python之字符串处理.1

作者: 知行鱼 | 来源:发表于2017-09-29 16:03 被阅读0次
    本文需要Python 3.0+
    

    函数format()可以用来很容易的对齐字符串。 你要做的就是使用<,>或者^字符后面紧跟一个指定的宽度。比如:

    >>>format(text,'>20')
    ' Hello World'
    >>>format(text,'<20')
    'Hello World        '
    >>>format(text,'^20')
    '    Hello World    '
    >>>
    

    如果你想指定一个非空格的填充字符,将它写到对齐字符的前面即可:

    >>>format(text,'=>20s')
    '=========Hello World'
    >>>format(text,'*^20s')
    '****Hello World*****'
    >>>
    

    当格式化多个值的时候,这些格式代码也可以被用在format()方法中。比如:

    >>>'{:>10s} {:>10s}'.format('Hello','World')
    '    Hello      World'
    >>>
    

    format()函数的一个好处是它不仅适用于字符串。它可以用来格式化任何值,使得它非常的通用。 比如,你可以用它来格式化数字:

    >>>x=1.2345
    >>>format(x,'>10')
    '    1.2345'
    >>>format(x,'^10.2f')
    '  1.23  '
    >>>
    

    以上内容引自 http://python3-cookbook.readthedocs.io/zh_CN/latest/c02/p13_aligning_text_strings.html

    使用以上方法我们打印一首带格式的诗:

    
    widthofline = 80
    
    print("\n")
    print(format(' The Zen of Python '.upper(), '#^{}s'.format(widthofline)))
    
    zenofpythons = '''
    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!
    '''
    
    lines = zenofpythons.split("\n")
    for line in lines:
        print("#", format(line, '^{}s'.format(widthofline-4)), "#")
    
    print("#" * widthofline)
    

    运行上面的程序,可以看到下面的结果:

    
    
    ############################## THE ZEN OF PYTHON ###############################
    #                                                                              #
    #                        Beautiful is better than ugly.                        #
    #                      Explicit is better than implicit.                       #
    #                        Simple is better than complex.                        #
    #                     Complex is better than complicated.                      #
    #                         Flat is better than nested.                          #
    #                         Sparse is better than dense.                         #
    #                             Readability counts.                              #
    #           Special cases aren't special enough to break the rules.            #
    #                     Although practicality beats purity.                      #
    #                      Errors should never pass silently.                      #
    #                         Unless explicitly silenced.                          #
    #          In the face of ambiguity, refuse the temptation to guess.           #
    #    There should be one-- and preferably only one --obvious way to do it.     #
    #      Although that way may not be obvious at first unless you're Dutch.      #
    #                          Now is better than never.                           #
    #               Although never is often better than *right* now.               #
    #          If the implementation is hard to explain, it's a bad idea.          #
    #       If the implementation is easy to explain, it may be a good idea.       #
    #       Namespaces are one honking great idea -- let's do more of those!       #
    #                                                                              #
    ################################################################################
    

    好玩吧?:) 有兴趣的可以把以上代码封装一下。

    相关文章

      网友评论

        本文标题:奇妙python之字符串处理.1

        本文链接:https://www.haomeiwen.com/subject/eibiextx.html