Ankky Ankky - 9 months ago 51
Python Question

Python prints text in next line

Hi I am reading a text from sample.txt which contains the value 'a'. But when I print it like this-

content = open(filepath,'r')
text = content.readlines()
print(text + ',')

Expected output -

Output obtained -


Because of this I am facing issue. Please help.

Answer Source

This is probably because there is \n character at the end of the line where a is found. Try using this:

print(text.strip() + ',')