使用fill char和counter创建python字符串

时间:2011-05-25 23:16:39

标签: python string

我想以优雅的方式做到这一点:

>>> ''.zfill(5, '-')
'-----'

有没有办法用填充字符和计数器初始化字符串? 当然,计数可能会有所不同。

2 个答案:

答案 0 :(得分:11)

试试吧:

>>> '-'*5
'-----'

在Python中就是这么简单:)

答案 1 :(得分:1)

introduction to strings in the "official" tutorial说:

  

字符串可以连接(粘合   与+运算符一起使用   重复*