Kodeclik Blog
How to print a line break in Python
When it comes to printing output in expressive ways, including line breaks or newlines, Python is a very versatile language offering a lot of options. In this blog post, we will explore different methods to print line breaks in Python, enabling you to format your output effectively and enhance the readability of your code.
Consider the following limerick:
Note that, like all limericks, we have five lines of output. How exactly do we print this limerick? Let us learn the ways.
Method 1: Use a newline character (“\n”)
Let us first try printing the limerick the regular way:
print("There once was a coder named Kodeclik,
whose skills were incredibly slick.
In Python they'd write,
for Minecraft, day and night,
Their code always did the trick!")
In the above program we use a single print() statement and have pressed carriage return inside the string. This program will however not quite work. It will produce an output like:
File "main.py", line 1
print("There once was a coder named Kodeclik,
^
SyntaxError: unterminated string literal (detected at line 1)
This is because the program is unable to interpret your string as containing new lines.
The simplest and most common way to print a line break in Python is by using the escape character "\n." When Python encounters this character in a string, it interprets it as a newline, causing the text following it to start on a new line. Here's an example: Thus, you can explicitly mention where you would like line breaks by using the “\n” character:
print("There once was a coder named Kodeclik,\\nwhose skills were
incredibly slick.\\nIn Python they'd write,\\nfor Minecraft, day
and night,\\nTheir code always did the trick!")
Now we have a program on a single line (make sure it is on one line rather than on multiple lines) but notice the “\n” characters. The output is what we expected:
There once was a coder named Kodeclik,
whose skills were incredibly slick.
In Python they'd write,
for Minecraft, day and night,
Their code always did the trick!
Method 2: Use the print() function with separate arguments
Python's print() function accepts multiple arguments, separated by commas. Further, by giving a suitable separation character as an argument,, you can achieve a line break. So we could have printed our limerick in the following way:
print("There once was a coder named Kodeclik,",
"whose skills were incredibly slick.","In Python they'd write,",
"for Minecraft, day and night,","Their code always did the trick!",
sep='\\n')
Note the “sep=\n” argument. Also note that we do not have explicit line breaks anywhere else in the print statement. This will again produce our expected output:
There once was a coder named Kodeclik,
whose skills were incredibly slick.
In Python they'd write,
for Minecraft, day and night,
Their code always did the trick!
Method 3: Use triple quotes delimiting your string with line breaks inside them
Triple quotes (''' or """) are primarily used to define multi-line strings in Python. However, they can also be utilized to print output with line breaks. By enclosing your text within triple quotes and manually inserting line breaks, you can control the format of your output. Here's an example:
print('''There once was a coder named Kodeclik,
whose skills were incredibly slick.
In Python they'd write,
for Minecraft, day and night,
Their code always did the trick!''')
The output will be:
There once was a coder named Kodeclik,
whose skills were incredibly slick.
In Python they'd write,
for Minecraft, day and night,
Their code always did the trick!
Printing line breaks in Python is essential for enhancing the readability and formatting of your output. Whether you're displaying information to users or organizing data within the console, utilizing line breaks makes your code more user-friendly.
Here, we have seen three different ways to print a line break in Python, including the escape character "\n," the print() function with multiple arguments and a “sep” argument, and triple quotes for multi-line strings. By mastering these methods, you can effectively control the flow of your output and create visually appealing results. Which of the methods introduced here is your favorite?
We hope you enjoyed learning about printing line breaks. If you liked this, learn about how to skip a line in Python.
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.