ljust/center/rjustの各関数jにおいて、第一引数に整形文字列の長さ、第二引数にパディング文字を指定する。
第二引数を省略した場合は、スペースが埋められる。
■サンプルプログラム
adjust.py
# 文字列の左寄せ/中央寄せ/右寄せを行う。 s = 'Python programming' print('デフォルトパディング') print('[', s.ljust(30), ']', sep='') print('[', s.center(30), ']', sep='') print('[', s.rjust(30), ']', sep='') print() print('パディング文字を指定する') print('[', s.ljust(30, '*'), ']', sep='') print('[', s.center(30, '*'), ']', sep='') print('[', s.rjust(30, '*'), ']', sep='')
■実行結果
$ python adjust.py デフォルトパディング [Python programming ] [ Python programming ] [ Python programming] パディング文字を指定する [Python programming************] [******Python programming******] [************Python programming]