How can I fill out a Python string with spaces?

前端 未结 13 1376
忘掉有多难
忘掉有多难 2020-11-22 07:13

I want to fill out a string with spaces. I know that the following works for zero\'s:

>>> print  \"\'%06d\'\"%4
\'000004\'

But wha

相关标签:
13条回答
  • 2020-11-22 07:53

    As of Python 3.6 you can just do

    >>> strng = 'hi'
    >>> f'{strng: <10}'
    

    with literal string interpolation.

    Or, if your padding size is in a variable, like this (thanks @Matt M.!):

    >>> to_pad = 10
    >>> f'{strng: <{to_pad}}'
    
    0 讨论(0)
提交回复
热议问题