带前导零的索引(数字格式的字符串连接)

index with leading zeros (string concatenation with numeric format)

我怎样才能得到这个索引列表,但带有前导零(“%04d”格式)?

["index{}".format(i) for i in range(20)]

想要:"index0001"、"index0002"、..

谢谢和亲切的问候

我认为 zfill 是您所需要的

["index{}".format(str(i).zfill(4)) for i in range(20)]