我想格式化一些輸出,我在屏幕上打印。這裏是我的代碼:Python str.format()方法不按預期工作
someString = someList[someIndex] + '{0:<8}'.format('\t') + someList[someOtherIndex]
print someString
我的預期輸出:
abcdefghi someOutput abcde someOtherOutput **OR** abcdefghi someOutput abcde someOtherOutput
實際輸出我得到:
abcdefghi someOutput abcde someOtherOutput
問題1:爲什麼我沒有得到預期的輸出?具體而言,爲什麼第二列中的條目錯位?
Q.2:我應該在代碼中更改哪些內容才能獲得預期輸出?
附加信息:我正在使用Python 2.6
感謝任何幫助。
meh我只是會做一些類似於'print'%-16s%s「%(」abcde「,」someOutput「)'...即使%格式被折舊了......它會在一段時間內出現 – 2012-07-27 18:29:50
但是爲什麼不學會使用推薦的str.format()? – NGambit 2012-07-27 18:33:19
我確定我會在需要時(例如,使用一種版本的python警告說這種方法的折舊或不再支持它的版本...它看起來不太硬..我只是喜歡舊版本更好,並會繼續使用它們,只要我使用支持它們的python – 2012-07-27 18:38:04