Python Cookbook学习记录 ch1_4_2013/10/22
1.4字符串对齐
使用string对象的ljust,rjust和center来实现
其中方法的第二个参数可以设置填充字符,默认为空格
>>> print '|'+'hello'.ljust(20)+'|' |hello | >>> print '|'+'hello'.rjust(20)+'|' | hello| >>> print '|'+'hello'.center(20)+'|' | hello | >>> print '|'+'hello'.center(20,'*')+'|' |*******hello********|
一个简单的输出:
>>> for item in range(5): print '|'+str(item).center(10)+'|' | 0 | | 1 | | 2 | | 3 | | 4 |
浙公网安备 33010602011771号