Python Program to Print Output in the Same Line

In this Python example, we will discuss can we get the output in the same line, which means that how can we prevent print to go to the next line.

Let’s get started.

1. How does print() works?

In Python, whenever we use the print function to print or give any output, it is designed to give output in the new line. The print function automatically goes to the next line. So, what can we do to prevent it from going to the next line?

To prevent the line to automatically go to the next line, we can use the additional parameter end=" " at the end of the print function. The second way is using the sys module in python. We will implement both ways.

Some of the topics which will be helpful for understanding the program implementation better are:

2. How to Print Output without using New Line in Python 3.X?

As discussed above, we will use the end parameter inside the print function to prevent the print function from automatically moving to the next line. Let’s see how it works.

#Python program to print output without new line 

print("codingeek", end = " ")
print("- Home for Coders")
codingeek - Home for Coders

In the above program, the next print statement will follow on the same line just after printing the codingeek.

Now, let’s suppose that we have a list that contains some strings. So, can we write all its output in the same line? The answer is YES. Let’s see how it works.

#Python program to print output without new line 
str_list = ["Powerful", "people", "comes", "from", "powerful", "places"]
# printing all element in same line
for word in range(len(str_list)):
  print(str_list[word], end =" ")
Powerful people comes from powerful places 

2.1. How to print or get output of all elements of list in one line in Python?

Are you tired of using loops again and again for printing each element in the list? and do you ask yourself the question: Why always loop to print all the elements of the list, isn’t there any easy way? The answer is YES.

In Python, we can print all the elements of the list in one single line without using any loop. Let’s see how can we do so.

#Python program to print all elements of list in single line
str_list = ["This", "list", "contains", "example", "which", "is", "to be", "printed"]
This list contains example which is to be printed

The *listname prints all elements in a single line.

3. How to print output without using new line in Python 2.X?

In the Python 2.X, in order to get the output or the string in a single line and preventing it from using the next new line, we will have to simply place a comma (,) at the end of the print statement. Let’s take an example to understand better.

#Python 2.X program to print all elements of list in single line

print "codingeek",
print "- Home for Coders"
codingeek - Home for Coders

4. How to print output in one line using sys module?

In Python, we can use the sys built-in module to print without using a newline. In order to use the sys module, we need to import the sys module using the import sys command.

Read more– about Python modules

The main method which is used from the sys module is the stdout.write() method. This will help to print the string in one line. Let’s implement a program to see how it actually works.

Do you want to know more about the sys module and its different methods in Python? If yes, then please refer to the official documentation.

import sys

sys.stdout.write("This line is first line. ")
sys.stdout.write("This is on the same line")
This line is first line. This is on the same line

5. Conclusion

In this article, we have discussed different programs on how we can print output in one line without using a new line (or without automatically moving to a new line).

Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

Recommended -

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x