0

(I checked " String formatting: % vs. .format vs. f-string literal " but that was different from my question I want a variable format but in that page all formats was static formats)

I hope you are well.

I wanna code a logging system for my program and it is something like this.

print("Checking list %02d of %02d parameter %02d of %02d)

That is in a loop and made output like this.

...
Checking list 09 of 22 parameter 35 of 36
Checking list 09 of 22 parameter 36 of 36
Checking list 10 of 22 parameter 01 of 41
Checking list 10 of 22 parameter 02 of 41
Checking list 10 of 22 parameter 03 of 41
...

If any list has more than 99 parameters it show like this.

...
Checking list 09 of 22 parameter 35 of 36
Checking list 09 of 22 parameter 36 of 36
Checking list 10 of 22 parameter 01 of 1427
Checking list 10 of 22 parameter 02 of 1427
Checking list 10 of 22 parameter 03 of 1427
...

Or there was more than 99 or less than 10 lists (It changes every time the program run!)

...
Checking list 99 of 218 parameter 129 of 130
Checking list 99 of 218 parameter 130 of 130
Checking list 100 of 218 parameter 765 of 767
Checking list 100 of 218 parameter 766 of 767
Checking list 100 of 218 parameter 767 of 767
...

And this is not what alignment I want! Is there any why that I could put variable instead 02 because every time I run the program number of lists and parameters will be changed and I cant do that with hard-coding. I wanna something like this (put a integer variable instead 02)

print("Checking list %(variable1)d of %(variable1)d parameter %(variable2)d of %(variable2)d)

variable1 must be the number of digits that last list number has and variable2 must be the number of digits that last parameter number has.

I want to notice that I can’t speak English well and I used Google Translate to write these I hope the text I wrote is understandable for you.

I will be grateful if you help me to solve this problem.

  • Use f-strings. https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings e.g. `print(f"Checking list {variable1} of {variable1} parameter ...")` – Samwise Aug 11 '21 at 17:56

0 Answers0