How to pad with n characters in Python

后端 未结 6 822
迷失自我
迷失自我 2020-12-28 13:29

I should define a function pad_with_n_chars(s, n, c) that takes a string \'s\', an integer \'n\', and a character \'c\' and returns a string consisting of \'s\'

6条回答
  •  情歌与酒
    2020-12-28 14:04

    With Python2.6 or better, there's no need to define your own function; the string format method can do all this for you:

    In [18]: '{s:{c}^{n}}'.format(s='dog',n=5,c='x')
    Out[18]: 'xdogx'
    

提交回复
热议问题