0

I would like to format a string based on the input. For example if the line count is 1, the output must be image00001.png and if the line count is 400, the output is image00400.png

I use the following to do so

filename = "image%05d.png".format(line_count)

but it doesn't work as expected. What am I missing?

Mr. Randy Tom
  • 311
  • 5
  • 13

4 Answers4

0

You confuse the notation for the substitute operator % and for the .format() method. The right use of the method is filename = "image{:05d}.png".format(line_count). An even better way is to use a format string: filename = f"image{line_count:05d}.png".

DYZ
  • 55,249
  • 10
  • 64
  • 93
0

In Python 3 you may use f-strings:

filename = f'image{line_count:05}.png'

Franz Diebold
  • 555
  • 6
  • 21
0

You are mixing different formatting specifications. For .format, use Format string syntax.

line_count = 1
filename = "image{:05d}.png".format(line_count)

or the new f-string method

line_count = 1
filename = f"image{line_count:05d}.png"
tdelaney
  • 73,364
  • 6
  • 83
  • 116
-1
fname = "image" + str(400).zfill(5) + ".png"
print(fname)
sushanth
  • 8,275
  • 3
  • 17
  • 28