Home > Net >  excess blank line is printing
excess blank line is printing

Time:07-20

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

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

CodePudding user response:

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()
  • Related