python - str.format() -> 如何左对齐

标签 python

>>> print 'there are {0:10} students and {1:10} teachers'.format(scnt, tcnt)
there are        100 students and         20 teachers

输出的代码是什么:

there are 100        students and 20         teachers

谢谢。

最佳答案

print 'there are {0:<10} students and {1:<10} teachers'.format(scnt, tcnt)

虽然老%运算符(operator)使用 -为了对齐,新的 format方法使用 <>

关于python - str.format() -> 如何左对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/829667/

相关文章:

python - 使用 Google Cloud Functions 长时间运行的 python 进程

python - 根据另一个列表更新列表中的项目

python - 为什么一个 python 文件设置了 indent-tabs-mode ?

python - flask_login 有时会混合登录;我以其他人身份登录

Python 太空入侵者与 PyGame 故障排除

python - 如何将数据帧转换为 csv 并保存以返回?

python - Scikit学习: TypeError: float() argument must be a string or a number,而不是 'Bunch'

python - 使selenium单击具有相同类别的连续元素n次

python - 临时目录在程序运行期间持续存在

Python TypeError : expected str, 字节或 os.PathLike 对象,而不是 _io.TextIOWrapper