How To Print New Line In Python?

New Line is a term used to describe jumping into the next or new line in a text or string. Newline specifies the end of the current line and called the end of the line in some programming languages. Newline can be used to set the end of the current text or show text in an elegant way without filling the screen completely.

New Line Characters

Python uses the /n as a new line character which consists of slash and n character. The new line characters or expressions can be used everywhere inside a string data or literal like the start of the string, middle of the string, or end of the string.

"This is the website\n"
"This is the \n website"
"\nThis is the website"

New Line In Terminal Output For Python Interactive Shell

Python provides an interactive shell in order to run and execute Python scripts. This interactive shell provides an interactive command-line wherein every statement new line is added automatically like below. But if you look following example the new line inside a string literal is not added to the interactive shell. If we create a string variable named s and put a new line while printing this s variable the new line printed too.

>>> "I like"+""
>>> "I like "+" "
'I like '
>>> "I like "+"\n "
'I like \n '
>>> s = "I like "+"\n "
>>> s
'I like \n '
>>> print(s)
I like 

New Line In A File

One of the most popular use cases for the new line is writing into the files. Especially text, code, script, or command files contains multiple lines and every line is created with the new line. The file.write() method is used to write new content to the opened file. By default, this method does not add the end of the line automatically. So we should add a new line by using the write() method like below.

file.write("I like")
file.write("I like")
file.write("I like \")

The file content will be like below.

I like PythonTect.comI like
I like

New Line with print() Method

The print() is one of the most used methods which will print given text or content or data into the standard output. Generally, the standard output will be a terminal or shell or a log file. The print() method provide

Print Without New Line

The print() method adds a new line at the end of the given string automatically and implicitly. Take a look to the following examples where every print() method has a new line.


The output will be like below where every print() method parameter is printed with a new line.


We can disable automatic new lines by using the print() methods end parameter by setting an empty string or whatever we want. By default, the end parameter gets \n as new line and this is added to the end of the content in every print() method. We can also add other characters to the end of the print() content by using the end parameter.

print("I like")

print("I like " , end="")

print("" , end="")

The output will be like below. We can see that we disable the end of line by setting empty the end parameter of the print() method.

I like
I like

os.linesep – Line Separator

The Python os module provides the linesep which returns the current platform and operating system line separator. The line separator or end of line or new line is the same thing which simply ends the current line and jumps to the next line. The POSIX operating systems like Linux distributions, BSD, Unix uses the “\n” as line separator but other operating systems like Windows uses “\r\n” as the line separator. The line separator or end of line character for the current platform can be printed like below.

import os


Leave a Comment