问题:
想要让输出的字符能够按照左对齐,右对齐,居中对齐的格式排列
解决方法:
在Python中,是用str的rjust,ljust,center方法来实现上述的功能.看下面的例子:
>>> print '|', 'hej'.ljust(20), '|', 'hej'.rjust(20), '|', 'hej'.center(20), '|'
| hej | hej | hej |
>>> print 'hej'.center(20, '+')
++++++++hej+++++++++
相关说明:
ljust(...)
S.ljust(width[, fillchar]) -> string
Return S left justified in a string of length width. Padding is
done using the specified fill character (default is a space).
rjust(...)
S.rjust(width[, fillchar]) -> string
Return S right justified in a string of length width. Padding is
done using the specified fill character (default is a space)
center(...)
S.center(width[, fillchar]) -> string
Return S centered in a string of length width. Padding is
done using the specified fill character (default is a space)
想要让输出的字符能够按照左对齐,右对齐,居中对齐的格式排列
解决方法:
在Python中,是用str的rjust,ljust,center方法来实现上述的功能.看下面的例子:
>>> print '|', 'hej'.ljust(20), '|', 'hej'.rjust(20), '|', 'hej'.center(20), '|'
| hej | hej | hej |
>>> print 'hej'.center(20, '+')
++++++++hej+++++++++
相关说明:
ljust(...)
S.ljust(width[, fillchar]) -> string
Return S left justified in a string of length width. Padding is
done using the specified fill character (default is a space).
rjust(...)
S.rjust(width[, fillchar]) -> string
Return S right justified in a string of length width. Padding is
done using the specified fill character (default is a space)
center(...)
S.center(width[, fillchar]) -> string
Return S centered in a string of length width. Padding is
done using the specified fill character (default is a space)