Inserting carriage return to string. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Inserting carriage return to string. (/thread-2358.html) |
Inserting carriage return to string. - rhubarbpieguy - Mar-09-2017 string='This is sentence 1.Ths is sentence 2.' print(string) produces: This is sentence 1.This is sentence 2. However, I'd like to print the string as separate lines. To do so I add what I believe are carriage returns as follows: string=This is sentence 1.\nThs is sentence 2.\n print(string) produces: This is sentence 1. Ths is sentence 2. That's what I want. But why does a loop print letter-by-letter? It seems it should also print everything up \n on one line. for i in string: print(i) T h i s i s s e n t e n c e 1 . T h i s i s s e n t e n c e 2 . RE: Inserting carriage return to string. - wavic - Mar-09-2017 Because you loop over each element of an iterable and print it. In this case, this is a string. A print function adds a new line character at the end. Just print it >>> string="This is sentence 1.\nThs is sentence 2.\n" >>> print(string) This is sentence 1. Ths is sentence 2. |