Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add space on a complex string?

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading