Home » Python » How would I specify a new line in Python?

How would I specify a new line in Python?

Posted by: admin November 1, 2017 Leave a comment

Questions:

How would I specify a new line in python?

For a comparison to Java in a String you would do something like “First Line\r\nSecond Line”

So How would you do that in Python? For purposes of saving files with more then one line.

Answers:

Depends on how correct you want to be. \n will usually do the job. If you really want to get it right you look up the newline character in the os package. (It’s actually called linesep.)

Note: when writing to files using the Python api, do not use the os.linesep. Just use \n, Python automatically translates that to the proper newline character for your platform.

Questions:
Answers:

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

Questions:
Answers:

You can either write in the new lines separately or in one string which is easier

example 1

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

example 2

Input

As others have pointed out above, place the \n at the relevant points in your string:

line = "hello how are you\n I am testing the new line escape sequence \n this seems to work"

file.write(line)

Output

hello how are you
I am testing the new line escape sequence
this seems to work

Questions:
Answers:

In Python you can just use the new-line character, i.e. \n

Questions:
Answers:

If you are entering several lines of text at once, I find this to be the most readable format.

file.write("\
Life's but a walking shadow, a poor player\n\
That struts and frets his hour upon the stage\n\
And then is heard no more: it is a tale\n\
Told by an idiot, full of sound and fury,\n\
Signifying nothing.\n\
")

The \ at the end of each line escapes the new line (which would cause an error).

Questions:
Answers:

The same way with '\n', though you’d probably not need the '\r'. Is there a reason you have it in your Java version? If you do need/want it, you can use it in the same way in Python too.

Questions:
Answers:

Most escape characters in string literals from Java are also valid in Python, such as “\r”, “\n”

Questions:
Answers:

If you only call

print

it will output a blank line. You can pipe the output to a file like this (considering your example):

f = open('out.txt', 'w')
print 'First line' >> f
print >> f
print 'Second line' >> f
f.close()

Not only is it OS-agnostic (without even having to use the os package), it’s also more readable than putting \n within strings.

Questions:
Answers:

having same question.
My code is

    #!/usr/bin/python

    print "Content-type: text/html\n\n"
    print 'First line \n Second line' 
    print "Hello World!" 

and my output is

First line Second line Hello World! 

why is my output on one line?