Removing extra spaces in print()

Question:

I want to show a progress like

[500/5000] [1000/5000] [1500/5000] ...

With the following print line

print("[",str(c),"/",str(len(my_list)),"] ", end='')

I see extra spaces like [ 1000 / 55707 ] [ 2000 / 55707 ]. I also tried

print('[{}'.format(c),"/",'{}]'.format(len(my_list))," ", end='')

But the output is [1000 / 55707] [2000 / 55707].

How can I I fix that?

Asked By: mahmood

||

Answers:

When you call print() with several arguments, they are separated by default by a space. You should call :

print("[",str(c),"/",str(len(my_list)),"] ", end='', sep='')

But the best and most modern way of doing is using f-strings :

print(f"[{c}/{len(my_list)}] ", end='')
Answered By: Gelineau
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.