How to print on the same line in Python – with example

  • Reading time:15 mins read

How to print on the same line in Python?

Python’s print function is perhaps the most used function in Python. The print function helps us display things on the standard output such as the console and by default, it ends in a new line.



So suppose we were printing two strings or variables, when we run the print function twice we would notice that it will automatically print them on separate lines.

This is because by default the print function comes with a parameter named ‘end’ whose default value is ‘/n’ the newline character in Python.



Perhaps one may think that if we go ahead and put the print statements on the same line and separate them with a comma, that maybe will put both of them on the same line. However, this actually doesn’t.



Now to override this behaviour it means that we have to specify a different value for the end parameter.

How to print on the same line in Python with the help of the “end” parameter

This is because although the print functions are still on the same line, each of the functions still has the ‘end’ parameter with ‘/n’ as the default value.


How to print on the same line in Python?


So what we are going to do is that instead of the end parameter defaulting to a new line we are going to pass whitespace.

This is going to make the print function not move to the next line but instead allows the second print function to display the second-string right next to the first one as shown here.



Now if we have something like a list or any other iterable we want to have the contents printed on one line.

Normally iterating through a list and printing out each individual item displays them on separate lines.


Print the list items on the same line

We can apply the same principle that we have discussed above to have the items of the list printed on the same line.


How to print on the same line in Python

So again, within the print function and just after ‘animal’ we will have a comma and then modify the end parameter to have whitespace using either single quotes or double-quotes.

This will then allow us to have the contents of the list displayed on a single line as shown below.


How to print on the same line in Python with the asterisk symbol

You would notice that at the end of the result we have this extra little character that looks a bit weird. Now to get around this, we can instead unpack the contents of the list using asterisks (*).

However, this method is only implemented in Python 3 therefore, we cannot use it in Python 2.



This method is precise and really useful when you’re trying to display some data in a certain way.


How to print on the same line in Python


So this is how to print on the same line in Python. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials


Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!





Not sure if programming is for you? With CodeBerry you’ll like it.