Ankky Ankky - 1 year ago 72
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() + ',')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download