Search code examples
pythonreadline

Python readline positioning my print underneath instead of next to


so i'd like to get the text saying Yuppie or Noup to the right side of the readline print and I can't quite figure out slicing and spacing yet. This task was about removing all special characters from a pre-existing .txt file, anyway heres the code:

import string
bringthe = open("ape.txt","r")
readthe = bringthe.readline()

invalid_char = set(string.punctuation)
while readthe:
    readthe = readthe[:-1]
    
    readthe = bringthe.readline()
    if any(poop in invalid_char for poop in readthe):
        print(readthe,'yuppie')
    else:
        print(readthe,'nouppie')

And this is what the outcome looks like, all the answers in the wrong position:

no2no123non4 

 nouppie

noq234n5ioqw#%

 yuppie

%#""SGMSGSER

 yuppie

doghdp5234

 nouppie

sg,dermoepm

 yuppie

43453-frgsd

 yuppie

hsth()))

 yuppie

bmepm35wae

 nouppie

vmopaem2234+0+

 yuppie

gsdm12313

 nouppie

bbrbwb55be3"?"#?

 yuppie

"?"#%#"!%#"&"?%%"?#?#"?"

 yuppie

retrte#%#?%

 yuppie

abcdefghijklmnopqrstuvxy  nouppie
 nouppie

Solution

  • The readline() function is keeping the newline at the end, you can remove it with rstrip():

    readthe = bringthe.readline().rstrip()