Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)
edited by

When compared to Java (in the case of a String), we will be doing something like below:

"First Line\r\nSecond Line"

How to do this in Python is my doubt. I am looking for this for the purpose of writing multiple lines on a regular file.

2 Answers

0 votes
by (106k points)
edited by

Below are some ways to specify a new line on Python:-

The new line character is \n. It is used inside a string.

Example:

print 'First line \n Second line'

where \n is the newline character.

This would yield the result:

First line Second line

To know more about this you can have a look at the following video tutorial:-

0 votes
by (108k points)

You can simply use the '\n' in Python:

Input

line1 = "hello how are you"

line2 = "I am testing the new line escape sequence"

line3 = "this seems to work"

You can write the '\n' separately:

file.write(line1)

file.write("\n")

file.write(line2)

file.write("\n")

file.write(line3)

file.write("\n")

Output: 

hello how are you

I am testing the new line escape sequence

this seems to work

Related questions

Browse Categories

...