-1

I am trying to place 2 sentences in continuation.

Output required:

Live simply that others may simply live. => LIVE SIMPLY THAT OTHERS MAY SIMPLY LIVE.

Output getting:

Real sign of intelligence isn't knowledge, it's imagination.

REAL SIGN OF INTELLIGENCE ISN'T KNOWLEDGE, IT'S IMAGINATION.

My code is:

with open("sentences.txt", "r") as readfile:
    for row in readfile:
        print(row,row.upper(),end='')
anand
  • 31
  • 5
  • I dont quite understand your exmaple, please try to rephrase the question. whats the input and desired ouput? – thenewguyhere Nov 10 '19 at 11:03
  • Please show your sentences.txt (or a shortened version if it is a long file) and the expected output. Also, I don't understand how making a sentence uppercase is "writing two sentences in front of each other". – actual_panda Nov 10 '19 at 11:06

3 Answers3

1

I think at the end of each line in your sentences.txt file, there is \n first you should remove \n from each line and then print it. here is the code:

row = row.replace('\n', '')
print(row,row.upper(),end='')

i just replace \n with nothing

Note: save your row in a array and then print all the elements of that array. you can see what characters are added at the end of the line. (for example: \r\n or just \n) for example:

array = row.split(' ') #split the line with space
Reyhaneh Torab
  • 360
  • 5
  • 9
0

you can write it like this :

with open("sentences.txt", "r") as readfile:
for row in readfile:
    print(row,'-->',row.upper(),end='')
zahra
  • 81
  • 1
  • 1
  • 6
0

As mentioned earlier, each line ends with special characters. These characters, e.g '\n' and '\r\n', result in a new line. So when you print row and row.upper(), you print a new line after each.

To fix this you need to strip these trailing special characters. one way is to use string.rstrip().

see https://stackoverflow.com/a/275025/5184851

So you can do something like:

with open("hi.txt", "r") as readfile:
    for row in readfile:
       print('=>'.join([x.rstrip() for x in [row, row.upper()]])) 
       # '=>' can be replaced with any delimator.