本文目录一览:
python如何左对齐输出数列?
list1=['你haodajerect',232,2323.434,32323,2332,23,23234,'ssdfsdf']
list2=['你',2,32,23.3434,3.2434323,2332,23,'s','ssdfsdf']
print("左对齐————————————————————")
for item1 in list1:
print(str(item1).ljust(15,'*'),end=' ')
print('\r\n')
for item2 in list2:
print(str(item2).ljust(15), end=' ')
print('\r\n右对齐————————————————————')
for item1 in list1:
print(str(item1).rjust(15,'*'),end=' ')
print('\r\n')
for item2 in list2:
print(str(item2).rjust(15), end=' ')
print('\r\n居中对齐————————————————————')
for item1 in list1:
print(str(item1).center(15,'*'),end=' ')
print('\r\n')
for item2 in list2:
print(str(item2).center(15,"*"), end=' ')
Python中print输入打印左对齐,右对齐,居中对齐等
python 将文本内容左对齐
test.py
------------------------------------------------------
def toleft(text):
lines = text.split('\n')
newlines = [line.strip() for line in lines]
return '\n'.join(newlines)
if __name__ == '__main__':
import sys
newcontent = toleft(open(sys.argv[1]).read())
open(sys.argv[1], 'w').write(newcontent)
------------------------------------------------------
该小程序在格式化后覆盖了原文件,请记得备份。
python print 左对齐
items = [
... ('data collector', 'OK'),
... ('prepair', 'Warning'),
... ('bind datas', 'Error'),
... ('output report', 'Fail'),
... ]
fmt = '%40s %-9s'
print '\n'.join([fmt % x for x in items])
data collector OK
prepair Warning
bind datas Error
output report Fail
fmt = '%-40s %9s'
print '\n'.join([fmt % x for x in items])
data collector OK
prepair Warning
bind datas Error
output report Fail
print '\n'.join([fmt % (x, '[%s]'%y) for x, y in items])
data collector [OK]
prepair [Warning]
bind datas [Error]
output report [Fail]