Ankky Ankky - 25 days ago 4
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 -
a,


Output obtained -

a
,


Because of this I am facing issue. Please help.

Answer

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

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