2012-03-21 15 views
3

私が知っているように、左右の両端揃えの文字列を塗りつぶしで印刷するには、いくつかのエレガントな方法があります。 "====中央揃えタイトル====" in python

str = "left_justified" 
str.ljust(20, '0'); 

または

print "{0:{1}<20}".format(str, "=") 

このような結果は、あなたが ^を逃した

答えて

5

を充填して、中央揃えの文字列を印刷するための最良の方法は何か

left_justified===== 

次のようになります。

s = 'centered' 
print "{0:{1}^20}".format(s, "=") 
# -> ======centered====== 

str変数の名前を、組み込みのstrをシャドーしないものに改名しました。

10
>>> "hello".center(50, '=') 
'======================hello=======================' 
2
>>> termwidth, fillchar = 78, '=' 
>>> print ' middle justified title '.center(termwidth, fillchar) 
=========================== middle justified title ===========================