How to Line Break in Python?
Last Updated :
29 Nov, 2024
In Python, line breaks are an essential part of formatting text output and ensuring that your code is readable and clean. Line breaks properly can make a huge difference in the clarity and functionality of your code.
In this guide, we will walk you through different ways to create line breaks in Python. Let’s explore the most common methods:
Line Breaks in Strings
Using \n
Escape Character
The simplest way to introduce a line break in Python is by using the \n
escape character. This tells Python to move the cursor to the next line wherever it appears in a string.
Example:
Python
In this example, the \n inside the string creates a line break between "Hello" and "World!"
Using Triple Quotes for Multi-line Strings
If you want to create a string that spans multiple lines, Python allows you to use triple quotes (''' or """) to define multi-line strings. This is especially useful when working with long text blocks or when formatting code cleanly.
Example:
Python
s = '''This is a
multi-line string.
Python makes it easy!'''
print(s)
OutputThis is a
multi-line string.
Python makes it easy!
Let's take a look at other cases of line break in python:
Line Breaks in print() Function
Using print() with Multiple Arguments
You can use the print() function in Python to print multiple strings on different lines without explicitly using \n. By default, the print() function adds a line break after each call.
Python
print("Hello")
print("World!")
Each print() statement outputs the string followed by a new line, so you don’t need to manually insert a \n.
Using print() Function with end
By default, the print() function in Python ends with a newline character (\n). If you wanted a custom line break, you could set the end argument to a newline or any other separator.
Python
print("Hello", end="\n\n") # Two line breaks after "Hello"
print("World")
Writing/Reading Line Breaks in Files
If you're working with files, you can use the write() method to add line breaks when saving text. Unlike print(), write() does not automatically add a newline at the end, so you need to manually include the \n if you want a line break.
Python
with open("example.txt", "w") as file:
file.write("Hello\nWorld\nPython\n")
This code will create a file example.txt
with the following contents:
Hello
World
Python
When reading files, Python automatically handles line breaks when using the readlines() method. Each line is stored as a string with its newline character \n still intact. You can remove the newline character by using the strip() method.
Similar Reads
Python - Line Break in String Line Break helps in making the string easier to read or display in a specific format. When working with lists, the join() method is useful, and formatted strings give us more control over our output.Using \n for new line The simplest way to add a line break in a string is by using the special charac
2 min read
How to Print a Tab in Python In Python, printing a tab space is useful when we need to format text, making it more readable or aligned. For example, when displaying data in columns, we might want to add a tab space between the values for a cleaner appearance. We can do this using simple methods like \t, the print() function or
2 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
Split and join a string in Python The goal here is to split a string into smaller parts based on a delimiter and then join those parts back together with a different delimiter. For example, given the string "Hello, how are you?", you might want to split it by spaces to get a list of individual words and then join them back together
3 min read