3 回答
TA贡献2037条经验 获得超6个赞
该选项卡工作正常,您可能正在寻找 .ljust(width)
lems = ['scaena', 'persona', 'improbus']
for i in lems:
print('{}{}'.format(i.ljust(10), 'whatever'))
打印出来:
scaena whatever
persona whatever
improbus whatever
有时计算字符串的最大长度很有用,因此输出更可靠
lems = ['scaena', 'persona', 'improbus']
max_len = max(len(l) for l in lems)
for i in lems:
print('{}{}'.format(i.ljust(max_len + 1), 'whatever'))
TA贡献1878条经验 获得超4个赞
我已经使用了这种结构:
tab = '\t'
file.write(f'''Name{tab}sn{tab}port{tab}sw_sn{tab}sw_port''')
TA贡献1828条经验 获得超6个赞
您还可以使用 f 字符串:
lems = ['scaena', 'persona', 'improbus']
for i in lems:
print(f"{i}\t{'whatever'}")
您可以使用该函数repr()查看该选项卡已添加到字符串中:
from reprlib import repr
lems = ['scaena', 'persona', 'improbus']
for i in lems:
print(repr(f"{i}\t{'whatever'}"))
输出:
'scaena\twhatever'
'persona\twhatever'
'improbus\twhatever'
添加回答
举报