I want to write to a file without adding a new line at the iterations of a for loop except the last one.
Code:
items = ['1','2','3']
with open('file.txt', "w") as f:
f.write('test' + '\n')
for t in items:
f.write(t + '\n')#i didnt know i could add the '\n'
f.write('test' + '\n')#here for it to work
for t in items:
f.write(t + '\n')
f.write('end')
Output in the file:
test
1
2
3
test
1
2
3
end
Output I want in the file:
test
123
test
123
end
I am new to python, so sorry for any inconstancies.