str.ljust 左对齐
字符串方法 str.ljust(),Python 官方文档描述如下:
help(str.ljust)
Help on method_descriptor:
ljust(self, width, fillchar=' ', /)
Return a left-justified string of length width.
Padding is done using the specified fill character (default is a space).
返回长度为 width 的字符串,原字符串在其中靠左对齐。使用指定的 fillchar 填充空位 (默认使用 ASCII 空格符)。如果 width 小于等于字符串长度 len(str) 则返回原字符串的副本。
'python'.ljust(1)
'python'
'python'.ljust(10,'~')
'python~~~~'
'python'.ljust(10)
'python '
str.center 居中
字符串方法 str.center(),Python 官方文档描述如下:
help(str.center)
Help on method_descriptor:
center(self, width, fillchar=' ', /)
Return a centered string of length width.
Padding is done using the specified fill character (default is a space).
返回长度为 width 的字符串,原字符串在其正中。使用指定的 fillchar 填充两边的空位(默认使用ASCII 空格符)。如果 width 小于等于字符串长度,则返回原字符串的副本:
'Python'.center(1)
'Python'
'Python'.center(10)
' Python '
'Python'.center(20,'~')
'~~~~~~~Python~~~~~~~'
str.rjust 右对齐
字符串方法 str.rjust(),Python 官方文档描述如下:
help(str.rjust)
Help on method_descriptor:
rjust(self, width, fillchar=' ', /)
Return a right-justified string of length width.
Padding is done using the specified fill character (default is a space).
返回长度为 width 的字符串,原字符串在其中靠右对齐。使用指定的 fillchar 填充空位 (默认使用 ASCII 空格符)。如果 width 小于等于字符串长度 len(str) 则返回原字符串的副本。
'python'.rjust(1)
'python'
'python'.rjust(10,'~')
'~~~~python'
'python'.rjust(10)
' python'