Python How to Print on the Same Line

To print on the same line in Python, add a second argument, end=’ ‘, to the print() function call.

For instance:

print("Hello world!", end = ' ')
print("It's me.")

Output:

Hello world! It's me.

This is the quick answer.

However, if you want to understand how this works, I recommend reading further.

Printing in Python

When writing a Python program, you commonly want to print stuff on the screen.

This is useful if you are debugging your program or testing how it works in general.

When you introduce consecutive print() function calls, Python automatically adds a new line.

However, sometimes this is not what you want.

In this guide, you will learn how to print on the same line in Python in a couple of easy ways.

Let’s briefly discuss strings before getting there. If you are a beginner, it is worthwhile to recap strings.

What Is a Python String

A Python string (str) is a common data type used to represent text.

To create a string, wrap letters or numbers around a set of quotes.

You can use either:

  1. Single quotes.
  2. Double quotes.

Here are some examples of strings:

s1 = "This is a test"
s2 = 'This is another test'

# To add quotes in a string, use double quotation marks with single-quote strings and vice versa.

q1 = "Then he said 'Bye'."
q2 = 'She told us "It is time to celebrate".'

Print without New Line in Python

To display strings in Python, you can print them into the console using the built-in print() function.

For example:

s1 = "This is a test"
s2 = 'This is another test'

print(s1)
print(s2)

Output:

This is a test
This is another test

Printing the strings this way introduces a new line.

Meanwhile, this is intuitive, it is not always what you want.

To make Python stop adding new lines when printing, you can specify another parameter inside the print() function.

This parameter is an optional parameter called end. It specifies the last character after printing the string.

By default, the end parameter is a newline character, that is, “\n”.

This is why there is always a new line after a print() function call.

But you can change the end parameter to whatever you want.

To not want to introduce a new line, replace the end with an empty space ” “.

For example:

s1 = "This is a test"
s2 = "This is another test"

print(s1, end=" ")
print(s2)

This results in both strings being printed on the same line:

This is a test This is another test

As another example, let’s set the end parameter such that a print() function call ends with 3 line breaks:

s1 = "This is a test"
s2 = "This is another test"

print(s1, end="\n\n\n")
print(s2)

This results in 3 new lines being printed in-between the two prints:

This is a test


This is another test

Now you know how to print strings on the same line in Python.

To be more formal, you now understand how to change the default end character of a print() function call.

Alternative Ways to Print on Same Line in Python

Let’s finally go through some alternative approaches to printing strings on the same line in Python.

Pass Multiple Strings into the Same print() Function Call

Python print() function accepts any number of positional arguments.

This means you can give it as many arguments as you want, and it will print them all into the console.

If you print multiple elements in the same print() function call, there will be no new lines added.

For instance:

s1 = "This is a test"
s2 = "This is another test"

print(s1, s2)

Output:

This is a test This is another test

Instead of adding a new line, a blank space is added. This makes printing multiple items in a single call behave in an intuitive way.

Join the Strings Before Printing

Another way to print strings on the same line in Python is by utilizing the string join() method.

The join() method follows this syntax:

separator.join(elements)

Where separator is a string that acts as a separator between each element in the elements list.

For example, you can join two strings by a blank space to produce one long string and print it.

Here is how it looks in code:

s1 = "This is a test"
s2 = "This is another test"

combined = " ".join([s1, s2])

print(combined)

Output:

This is a test This is another test

However, technically this is not printing on the same line. Instead, we combine two strings into one string and print it as a single string.

Now you know a bunch of ways to print strings on the same line in Python.

Last but not least, let’s see how things work in older versions of Python 2.

How to Print on the Same Line in Python 2.x

In Python 2 print is not a function but a statement.

To print on the same line using Python 2.x, add a comma after the print statement.

For example:

s1 = "This is a test"
s2 = "This is another test"

print s1,
print s2

Output:

This is a test This is another test

Conclusion

Today you learned how to print on the same line in Python.

To recap, the print() function adds a newline character to the end by default. This means each print() call prints into a new line.

To not print a new line, you can choose one of these three options:

  1. Specify an optional parameter end as a blank space.
  2. Call the print() function with multiple inputs.
  3. Use string’s join() method to combine the strings before printing.

Thanks for reading.

Happy coding!

Further Reading

All String Methods in Python

50 Python Interview Questions and Answers