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 pythontect.com\n" "This is the \n website pythontect.com" "\nThis is the website pythontect.com"
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"+"PythonTect.com"
'I likePythonTect.com'
>>>
>>> "I like "+" PythonTect.com "
'I like PythonTect.com '
>>>
>>> "I like "+"\n PythonTect.com "
'I like \n PythonTect.com '
>>>
>>> s = "I like "+"\n PythonTect.com "
>>>
>>> s
'I like \n PythonTect.com '
>>> print(s)
I like
PythonTect.com
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 PythonTect.com")
file.write("I like PythonTect.com")
file.write("\n")
file.write("I like \nPythonTect.com")
The file content will be like below.
I like PythonTect.comI like PythonTect.com I like PythonTect.com
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.
print("I")
print("like")
print("PythonTect.com")
The output will be like below where every print() method parameter is printed with a new line.
I like PythonTect.com
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 PythonTect.com")
print("I like " , end="")
print("PythonTect.com" , 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 PythonTect.com I like PythonTect.com
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
print(os.linesep)