Mar-09-2017, 09:35 PM
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
.
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
.