How do you print on the same line in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Generally, people switching from C/C++ to Python wonder how to print two or more variables or statements without going into a new line in python. Since the python print() function by default ends with a newline. Python has a predefined format if you use print(a_variable) then it will go to the next line automatically. 
     

    For example: 

    Python3

    print("geeks")

    print("geeksforgeeks")

    Will result in this: 

    geeks
    geeksforgeeks

    But sometimes it may happen that we don’t want to go to the next line but want to print on the same line. So what we can do? 
     

    For Example: 

    Input : print("geeks") print("geeksforgeeks")
    Output : geeks geeksforgeeks
    
    Input : a = [1, 2, 3, 4]
    Output : 1 2 3 4 

    The solution discussed here is totally dependent on the python version you are using. 
     

    python

    print("geeks"),

    print("geeksforgeeks")

    a = [1, 2, 3, 4]

    for i in range(4):

        print(a[i]),

    Output: 

    geeks geeksforgeeks
    1 2 3 4

    python3

    print("geeks", end =" ")

    print("geeksforgeeks")

    a = [1, 2, 3, 4]

    for i in range(4):

        print(a[i], end =" ")

    Output: 

    geeks geeksforgeeks
    1 2 3 4

    Python3

    Output:

    1 2 3 4 5 6


    The print() method in Python automatically prints in the next line each time. The print() method by default takes the pointer to the next line.

    Example

     Live Demo

    for i in range(5):
       print(i)

    Output

    0
    1
    2
    3
    4

    Modify print() method to print on the same line

    The print method takes an extra parameter end=” “ to keep the pointer on the same line.

    The end parameter can take certain values such as a space or some sign in the double quotes to separate the elements printed in the same line.

    Syntax

    print(“…..” , end=” “)

    The end=” “ is used to print in the same line with space after each element. It prints a space after each element in the same line.

    Example

     Live Demo

    for i in range(5):
       print(i,end=" ")

    Output

    0 1 2 3 4

    The end=”” is used to print on same line without space. Keeping the doube quotes empty merge all the elements together in the same line.

    Example

     Live Demo

    for i in range(5):
       print(i,end="")

    Output

    01234

    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.

    Example

     Live Demo

    for i in range(5):
       print(i,end=",")
       print(i,end=".")

    Output

    0,1,2,3,4,
    0.1.2.3.4.

    How do you print on the same line in python?

    Updated on 10-Mar-2021 14:07:42

    • Related Questions & Answers
    • How to print new line in Java?
    • How to Pretty print Python dictionary from command line?
    • How to print a blank line in C#?
    • How to print matrix without line numbers in R?
    • How can we combine multiple print statements per line in Python?
    • How to capture multiple matches in the same line in Java regex
    • How to create two line charts in the same plot in R?
    • Print level order traversal line by line in C++ Programming.
    • How to print pattern in Python?
    • Print new line and tab in Arduino
    • How to compare two different files line by line in Python?
    • How to execute Python multi-line statements in the one-line at command-line?
    • How to print a line on the console using C#?
    • How to use GridBagConstraints to layout two components in the same line with Java
    • How to print concatenated string in Python?

    How do I print the same line multiple times in Python?

    Use range() to print a string multiple times Use range(stop) to create a range of 0 to stop where stop is the number of lines desired. Use a for-loop to iterate through this range. In each iteration, concatenate the string to itself by the number of times desired and print the result.

    How do you print side by side in Python?

    Using "end" Argument in the print statement (Python 3. X) In Python 3, print() is a function that prints output on different lines, every time you use the function. ... .
    Using "sys" Library (Python 3. X) to Print Without Newline. ... .
    Using "comma" to Terminate Print Statement. In order to print in the same line in Python 2..

    How do I stop the next line from printing in Python?

    Printing without a new line is simple in Python 3. In order to print without newline in Python, you need to add an extra argument to your print function that will tell the program that you don't want your next string to be on a new line. Here's an example: print("Hello there!", end = '') print("It is a great day.")