How do you print multiple items on the same line in python?

The Python's print() function is used to print the result or output to the screen. By default, it jumps to the newline to printing the next statement. It has a pre-defined format to print the output. Let's understand the following example.

Example - 1

Output:

Or, we can write the complete statement in single print() function.

Output:

The Python print() function has an argument called end, which prevents jump into the newline. Let's understand the following example.

Example - 3:

Output:

In the above code, we declared a list and iterated each element using for loop. The print() function printed the first element of the list and then printed the end value which we assigned as ' ' whitespace and it will be printed till the element of the list.

We can assign any literal to the end. Let's understand the following example.

Example - 4

Output:


  1. HowTo
  2. Python How-To's
  3. Print on the Same Line in Python

Created: February-14, 2021 | Updated: March-05, 2021

  1. Multiple Prints on the Same Line in Python Using the print() Function
  2. Multiple Prints on the Same Line in Python Using the stdout.write() Method of the sys Module in Python

This tutorial will explain various methods to print multiple things on the same line in Python. Usually, the print() method prints the content in the new line each time. We can use the following methods to print multiple things on the same line in Python.

Multiple Prints on the Same Line in Python Using the print() Function

The print method takes a string or any valid object as input, converts it into a string, and print it on the screen. To print multiple things on the same line using the print function in Python, we will have to follow different ways depending on the Python version.

Python 2.x

In Python 2.x, we can put , at the end of the print method to print multiple times on the same line. The following example code demonstrates how to the print function for this.

print 'hello...',
print 'how are you?'

Output:

hello...how are you?

Python 3.x

And in Python 3.x, we will have to change the value of the end parameter of the print() method, as it is set to the \n by default. The example code below demonstrates how we can use the print() method with the end parameter set as "" to print multiple times on the same line.

print('hello...', end=""),
print('how are you?')

Output:

hello...how are you?

Multiple Prints on the Same Line in Python Using the stdout.write() Method of the sys Module in Python

The stdout.write() method of the sys module prints the output on the screen. As the stdout.write() does not add the new line at the end of the string by default, it can be used to print multiple times on the same line.

Unlike the print() method, this method works on all the Python versions, but we will need to import the sys module first to use the stdout.write() method. The example code below shows how to use the stdout.write() method to print multiple strings on the same line in Python.

import sys

sys.stdout.write('hello...')
sys.stdout.write('how are you?')

Output:

hello...how are you?

Related Article - Python Print

  • Print Multiple Arguments in Python
  • Print With Column Alignment in Python
  • Print Subscripts to the Console Window in Python
  • Print Quotes in Python
  • How do you print multiple items on the same line in python?

    How do you print multiple items on the same line in python?

    Printing one variable is an easy task with the print() function but printing multiple variables is not a complex task too. There are various ways to print the multiple variables.

    To print multiple variables in Python, use the print() function. The print(*objects) is a built-in Python function that takes the *objects as multiple arguments to print each argument separated by a space.

    There are many ways to print multiple variables. A simple way is to use the print() function.

    band = 8
    name = "Sarah Fier"
    
    print("The band for", name, "is", band, "out of 10")

    Output

    The band for Sarah Fier is 8 out of 10

    In this code, we are printing the following two variables using the print() function.

    1. band
    2. name

    Inside the print() function, we have put the variable name in their respective places, and when you run the program, it reads the values of the variables and print their values.

    This is the clearest way to pass the values as parameters.

    Using %-formatting

    Let’s use the %-formatting and pass the variable as a tuple inside the print() function.

    band = 8
    name = "Sarah Fier"
    
    print("The band for %s is %s out of 10" % (name, band))

    Output

    The band for Sarah Fier is 8 out of 10

    Pass it as a dictionary

    You can pass variables as a dictionary to the print() function.

    band = 8
    name = "Sarah Fier"
    
    print("The band for %(n)s is %(b)s out of 10" % {'n': name, 'b': band})

    Output

    The band for Sarah Fier is 8 out of 10

    Using new-style formatting

    With Python 3.0, the format() method has been introduced for handling complex string formatting more efficiently. Formatters work by putting in one or more replacement fields and placeholders defined by a pair of curly braces { } into a string and calling the string.format().

    It is a new style of string formatting using the format() method. It is useful for reordering or printing the same one multiple times.

    band = 8
    name = "Sarah Fier"
    
    print("The band for {} is {} out of 10".format(name, band))

    Output

    The band for Sarah Fier is 8 out of 10

    Python f-string

    We can use the f-string to print multiple variables in Python 3. Python f String is an improvement over previous formatting methods.

    It is also called “formatted string literals,” f-strings are string literals that have an f at the starting and curly braces containing the expressions that will be replaced with their values.

    band = 8
    name = "Sarah Fier"
    
    print(f"The band for {name} is {band} out of 10")

    Output

    The band for Sarah Fier is 8 out of 10

    That’s it for this tutorial.

    How to Print Type of Variable in Python

    How to Print an Array in Python

    How to Print Bold Python Text

    How do you print multiple items on one line in Python?

    To print on the same line in Python, add a second argument, end=' ', to the print() function call. print("It's me.")

    How do you print two outputs on the same line in Python?

    The end=”,” is used to print in the same line with a comma after each element. We can use some other sign such as '. ' or ';' inside the end parameter.

    How do you print multiple objects in Python?

    Use print() to print multiple variables Use print(*objects) with *objects as multiple arguments to print each argument separated by a space. Further Reading: Use * to pass in an arbitrary number of arguments to a function.

    How do you print side by side in Python?

    side_by_side allows users to print two multi-line outputs side-by-side. This produces an effect similar to running diff -y file1 file2 in a Unix system.