Python How to Print on the Same Line

print on the same line in Python

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

For instance:

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

Output:

Hello world! It's me.

Printing in Python

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

When you use multiple print() functions, 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.

What Is a Python String

Before jumping into details about printing on the same line, let’s quickly remind ourselves what is a string in Python.

A Python string is a common data type used to represent text. A string is a sequence of letters, numbers, symbols, and spaces.

In Python, you cannot just write a word as is. Instead, you have to create a string to represent the word.

To create a string, wrap the word/words around a set of quotes. You can use either one of the two:

  1. Single quotes.
  2. Double quotes.

For example, let’s create a bunch 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.

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

But as you can see, printing the strings this way introduces a new line. Meanwhile, it 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”. But you can change it to whatever you want. As you do not want to introduce new lines, you may replace the end character with an empty space ” “.

Let’s see an 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

Notice that the end character can be anything.

For example, you can add three newline characters (“\n”) if you wish to:

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

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

This results in three new lines being printed in-between the two strings:

This is a test


This is another test

Good! Now you know a way to print strings on the same line in Python.

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() accepts any number of positional arguments. This means you can input 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 between the strings, a blank space is added by default. This makes printing multiple items in a single call intuitive as there are no newlines.

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

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

For the sake of completeness, let’s also quickly show how you can print on the same line with older versions of Python.

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

Share on facebook
Share on twitter
Share on linkedin

Leave a Comment

Your email address will not be published.