使用变量格式对齐?

ves*_*che 4 python format variables alignment python-2.7

我正试图通过变量对齐来对齐一些文本.

例如,这有效:

>>> print '{:>10}'.format('foo')
       foo
Run Code Online (Sandbox Code Playgroud)

但这不是:

>>> x = 10
>>> print '{:>x}'.format('foo')
Run Code Online (Sandbox Code Playgroud)

xiº*_*xiº 13

检查文档:

您正在寻找:

>>> print '{0:>{x}}'.format('foo', x=x)
       foo
Run Code Online (Sandbox Code Playgroud)