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
  • 151
  • 2
  • 9

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
  • 51,549
  • 10
  • 60
  • 87
0

In Python 3 you may use f-strings:

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

Franz Diebold
  • 515
  • 5
  • 20
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
  • 63,514
  • 5
  • 71
  • 101
-1
fname = "image" + str(400).zfill(5) + ".png"
print(fname)
sushanth
  • 8,114
  • 3
  • 15
  • 27