How to Print in the Same Line in Python [6 Methods]

Are you finding a solution to print multiple lines in one line? Let me tell you different ways of “How to print in the same line in Python” with some demonstrative examples.

Being a Python developer, when I got a requirement to print lines in one line in Python, I found six ways to do so. In this Python tutorial, let’s learn about all the methods in detail.

In Python 3.x we can use the print() function with the “end” parameter to print in a single line, or by passing multiple variables as arguments. We can also use the for loop to iterate over each variable, print without any new line, etc.

Print in the Same Line in Python

Here is the list of all the six methods that can be used to print in same line in Python:

  1. print() function with end parameter
  2. print() function with multiple arguments
  3. Using for loop
  4. while loop
  5. join() function
  6. Concatenation operator

Let’s see them one by one using some illustrative examples:

1. Python Print Without New Line using the print() function with end parameter

The print() function in Python has an optional “end parameter“, which allows us to specify the character that should be printed at the end of the line while printing.

By default, it is set to a newline character (‘\n‘). To print in the same line in Python, we can set the “end” parameter to an empty string or any symbol that we want. An example is as follows:

Statement1 = "Our liberties we prize."
Statement2 = "Our rights we will maintain."
print(Statement1, end=' ')
print(Statement2)

Output: I have set the end parameter to a space so that the next print() function can start after that space.

Our liberties we prize. Our rights we will maintain.

Here is the screenshot taken after the execution of the code:

How to Print in the Same Line in Python

2. Python print without new line using the print() function with multiple arguments

The print() function in Python can take multiple arguments at a time. So, we can pass multiple arguments to the print() function, and they will automatically get concatenated with a space. This allows us to print in the same line in Python.

Here is an example to print without a newline in Python:

Statement1 = "Never judge a book by its cover."
Statement2 = "Honesty is the best policy."
print(Statement1, Statement2)

Output: I have simply passed my two statement variables to the print() function.

Never judge a book by its cover. Honesty is the best policy.

The screenshot of the output:

how to print multiple lines in one line Python

3. Python print in one line using for loop

The for loop in Python is used to iterate through a sequence. We can use the for loop, and iterate over a sequence. And then print each element in that sequence with the end parameter, to print without a newline in Python.

Here is an example:

USA = ["United", "States", "of", "America"]

for x in USA:
    print(x, end=" ")

Output:

United States of America

After implementing the code, the below screenshot is taken:

how to print on the same line in python using for loop

4. Python print on the same in the while loop

We can use the while loop as well to iterate over a sequence and print it with the end parameter for Python to print in the same line without overwrite.

Here is an instance, in Python print in the same line in the loop:

count = 1

while count <= 5:
    print("Day", count, end=" ")
    count += 1

Output:

Day 1 Day 2 Day 3 Day 4 Day 5 

Following is the screenshot taken after running the script:

Python print on the same line overwrite

5. Python print in the same line using the join() function

We can concatenate elements of a sequence into a single string using the join() function in Python. This enables the printing of multiple items in the same line with a specified separator.

For example:

cities = ["New", "York", "Los", "Angeles"]
print(", ".join(cities))

Output: We are joining all the elements with the “, “.

New, York, Los, Angeles

The output is shown below as a screenshot also:

python print in same line without space

6. Print in same line in Python using the concatenation operator

We can use the concatenation operator(+) to concatenate lines in Python, and then print it. We will get the multiple lines online without newline in Python.

P = "Python"
G = "Guides"
print(P+G)

Output: I have just taken two variables, one can take as many as he/she wants.

PythonGuides

Here is the screenshot of the script implementation in Pycharm editor.

How to print without newline in Python

Conclusion

Understanding different ways like print() function with end parameter or multiple arguments, using for loop, while loop, join() function and concatenation operator for “how to print in the same line in Python.” One can easily solve his problem or can execute his program accordingly.

The choice of the methods depends on the requirements of one’s program. I have explained every method with examples to print in the same line in Python.

You may also like to read: