Home > database >  Removing extra spaces in print()
Removing extra spaces in print()

Time:01-17

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?

CodePudding user response:

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='')
  • Related