0 votes
1 view
in Python by (47.8k 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 (107k 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 (96.7k 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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...