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 consist of slash and n character. The new line characters or expression can be used everywhere inside a string data or literal like 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 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 the one of the most used method 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() method
end parameter by setting empty string or whatever we want. By default the end parameter gets
\n 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.
I like PythonTect.com I like PythonTect.com