Pyderman
Pyderman

Reputation: 16199

Can a variable be passed to format() to pad?

With new-style formatting, we can do:

In [262]: '{:_>10}'.format('test')
Out[262]: '______test'

Instead of the underscore (or whatever character), can this be replaced by a variable? So if:

double_dashes = '--'

Can we somehow incorporate this variable in call to format() so we get:

--------------------test 

Upvotes: 3

Views: 127

Answers (1)

Robᵩ
Robᵩ

Reputation: 168626

Instead of the underscore (or whatever character), can this be replaced by a variable?

Yes, that is fairly easy using a nested {}:

>>> '{:{}>10}'.format('test', 'x')
'xxxxxxtest'

Can we somehow incorporate this variable in call to format() so we get:

--------------------test 

No. The fill character string must only be one character long.

Upvotes: 3

Related Questions