0
with open("data.txt",'r') as f:
    player1 = f.readline()
    player2 = f.readline()

print (player1+" you are blue")
print (player2+" you are red")

my text file is

sam
jim 

both are on a seperate line

currently it is outputing as

sam
 you are blue
jim
 you are red

I want it to output

sam you are blue
jim you are red

I have had a look around but can't seem to find an answer I have also tried a .split and do a word count but I couldnt get it working

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • You'll find your answer [here](https://stackoverflow.com/questions/12330522/how-to-read-a-file-without-newlines). The problem is when you read the line "sam", it is actually read as "sam\n" where "\n" indicates a new line. You need to `strip` the new line off of the text so it just reads "sam". – Kraigolas Feb 27 '21 at 03:58

1 Answers1

0

you need to use strip to remove the trailing new line

with open ("data.txt",'r') as file:
    player1 = file.readline()
    player2 = file.readline()
    
print(player1.strip() + " you are blue" )
print(player2.strip() + " you are red")
omar magdi
  • 36
  • 3
  • thankyou the .strip() worked for me I didnt know about it I had to change where I put it in my code as I didnt put all 400lines of my code in it as it was just that bit that I coundnt do – nuttyknife Feb 27 '21 at 04:18