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

String format empty string caused extra space in print

I want to string format a sentence as below:

integer = 1
if integer != 1:
    n_val, book = integer, 'books'
else:
    n_val, book ='', 'book'

print(f'Fetch the top {n_val} children {book}.')

and I expected to see:

Fetch the top 3 children books.

or

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

Fetch the top children book.

It works if integer is not 1, however, when integer =1, the string format gives me an additional space in the place of integer as shown below:

Fetch the top  children book.

How do I get rid of the space when integer =1?

>Solution :

Make the space part of the variable.

integer = 1
if integer != 1:
    n_val, book = ' '+str(integer), 'books'
else:
    n_val, book ='', 'book'

print(f'Fetch the top{n_val} children {book}.')
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