Home > Back-end >  How to add space on a complex string?
How to add space on a complex string?

Time:06-21

I have this string that act's like a progress bar

etichetta_download_mp3["text"] = '\r'   'Download : %s%s%.2f%% ' % ('█' * int(size * 20 / contentsize), '' * (20 - int(size * 20 / contentsize)), float(size / contentsize * 100))

And looks like this : enter image description here

Please could you explain me how to add a single space like " " in the red arrow? Like end of the bar, space and then the 100 % percentage, because in my image they are too closed, probably it's an easy task but i can't understand how to do it ... thanks!

CodePudding user response:

One format that should work is probably 'Download : %s%s %.2f%%'. The space should be before %.2f since that's where your percentage value is being formatted.

Example here:

# some dummy values
size = 1
contentsize = 5

fmt = 'Download : %s%s %.2f%%' % ('█' * int(size * 20 / contentsize), '' * (20 - int(size * 20 / contentsize)), float(size / contentsize * 100))

print(fmt)

Output:

Download : ████ 20.00%

Edit: also, I realized your second %s is unnecessary because you're trying to insert '' * (20 - int(size * 20 / contentsize)), but '' is zero-length, so it doesn't actually add anything. This would do the equivalent of what you're trying to achieve (notice one fewer %s, and removing the second calculation you have).

# some dummy values
size = 1
contentsize = 5

fmt = 'Download : %s %.2f%%' % ('█' * int(size * 20 / contentsize), float(size / contentsize * 100))

print(fmt)
  • Related