奇妙python之字符串处理.1

本文需要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!       #
#                                                                              #
################################################################################

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容