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

excess blank line is printing

so I want to read a text file and print its all content in uppercase
character. I written the code and it’s correct also. But I don’t know
why the excess blank line is printing between the two line.

import pickle
file=open("STORY.TXT",'r')
string=file.readlines()
for x in string:
    print(x.upper())
file.close()

output

@AN ORANAGE IS WRONG

@WRONG

@APPLE 

@IS GOOD

@FOR YT IN SE

@AA

@AA 

desired output

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

@AN ORANAGE IS WRONG
@WRONG
@APPLE 
@IS GOOD
@FOR YT IN SE
@AA
@AA

>Solution :

When you use .readlines without arguments you get trailing newlines, print add newlines by default, hence blank lines. You should instruct print to not add anything, that is do

file=open("STORY.TXT",'r')
string=file.readlines()
for x in string:
    print(x.upper(),end="")
file.close()
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