Search code examples
pythonpython-2.7variablesformatalignment

Format align using a variable?


I'm trying to right align some text, with a variable alignment.

For instance this works:

>>> print '{:>10}'.format('foo')
       foo

But this does not:

>>> x = 10
>>> print '{:>x}'.format('foo')

Solution

  • Check docs:

    You are looking for:

    >>> print '{0:>{x}}'.format('foo', x=x)
           foo