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:
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:
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:
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:
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:
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:
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:
The output will be:
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?
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.