Python New Line - Add/Print a new line
Last Updated :
10 Apr, 2025
Adding or printing a new line in Python is a very basic and essential operation that can be used to format output or separate the data for better readability. Let's see the different ways to add/print a new line in Python
Using \n escape character
The \n is a escape character and is the simplest way to insert a new line in Python.
Python
Explanation: Whenever \n is included in a string, it moves the cursor to the next line, hence adding/printing the rest of the output in a new line.
Using print() function with a new line
Using multiple print() statements to simply print a new line.
Python
print("Hello")
print("World")
Explanation: The print() function in Python ends with a new line so we don't need to do anything to create a new line between print statements.
Adding new lines to Strings
We can concatenate multiple strings by using '\n' between them directly to include new lines.
Python
a = "Hello" + "\n" + "World"
print(a)
Explanation: \n moves the cursor to the next line, thus printing whatever's left in the next line.
Using Triple Quotes
Using triple quotes( ' ' ' or " " ") allows us to include new lines directly in the string without using \n.
Python
a = """Hello
World"""
print(a)
Explanation: Whenever we enclose a string in triple quotes any line breaks, tabs or spaces within the quotes are preserved as they are.
The os.lineup constant provides us with the newline character that is specific to the operating system.
Python
import os
# Add new lines using os.linesep
a = "Hello," + os.linesep + "This is a new line."
print(a)
OutputHello,
This is a new line.
Explanation: This code uses os.linesep to add a platform-specific newline between the strings "Hello," and "This is a new line.", and then prints the concatenated message.
Similar Reads
Print new line in Python In this article, we will explore various methods to print new lines in the code. Python provides us with a set of characters that performs a specific operation in the code. One such character is the new line character "\n" which inserts a new line. Pythona = "Geeks\nfor\nGeeks" print(a)OutputGeeks f
2 min read
Print a List in Horizontally in Python Printing a list horizontally means displaying the elements of a list in a single line instead of each element being on a new line. In this article, we will explore some methods to print a list horizontally in Python.Using * (Unpacking Operator)unpacking operator (*) allows us to print list elements
2 min read
Print the Content of a Txt File in Python Python provides a straightforward way to read and print the contents of a .txt file. Whether you are a beginner or an experienced developer, understanding how to work with file operations in Python is essential. In this article, we will explore some simple code examples to help you print the content
3 min read
Python - Add List Items Python lists are dynamic, which means we can add items to them anytime. In this guide, we'll look at some common ways to add single or multiple items to a list using built-in methods and operators with simple examples:Add a Single Item Using append()append() method adds one item to the end of the li
3 min read
How to Print without newline in Python? In Python, the print() function adds a newline by default after each output. To print without a newline, you can use the end parameter in the print() function and set it to an empty string or a space, depending on your needs. This allows you to print on the same line. Example:Pythonprint("geeks", en
3 min read
Append Text or Lines to a File in Python Appending text or lines to a file is a common operation in programming, especially when you want to add new information to an existing file without overwriting its content. In Python, this task is made simple with built-in functions that allow you to open a file and append data to it. In this tutori
3 min read