How To Print Multiple Lines In One Line Python

In this tutorial, we will learn how to print multiple lines of text in one line using Python. This is a helpful technique if you want to display information in a more organized and compact way, especially when working with large outputs or when printing lists and dictionaries inside loops.

Step 1: Using end parameter in print()

By default, the Python print() function adds a newline character at the end of the printed text, which causes the next print statement to start on a new line. However, you can manipulate the end parameter inside the print function to control the behavior of line breaks after printing.

Let’s see the following example:

In this example, we are telling Python not to add a newline character at the end of the first print statement by using the end parameter and setting it to an empty string. The output would be:

Hello, world!

Step 2: Using the join() method

The join() method is a string method in Python that allows you to concatenate multiple strings from an iterable (e.g., list or tuple) with a specified separator. You can use this method to print multiple lines in one line. Let’s consider an example:

In this case, the join() method takes a list of strings and concatenates them using a space as the separator. The output would be:

Python is awesome!

You can use any separator you want, such as a comma, tab, or even an empty string.

Step 3: Using list comprehension and join()

Sometimes, you may want to apply a specific operation to each element in the iterable before printing them in one line. In this case, you can use list comprehension along with the join() method. Let’s see an example:

Here, we are using list comprehension to calculate the square of each number in the list and convert it to a string. Then, the join() method concatenates these strings with a comma separator. The output would be:

1, 4, 9, 16, 25

Full Code


In this tutorial, we have learned how to print multiple lines of text in one line using Python by manipulating the end parameter in the print() function, using the join() method, and combining list comprehension with the join() method.

These techniques are useful for displaying information in a more organized and compact way. Practice these methods and enhance your Python programming skills.