How to go to next line in python print

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

    Python print() built-in function is used to print the given content inside the command prompt. The default functionality of Python print is that it adds a newline character at the end.

    In this Python tutorial, you will learn:

    • Working of Normal print() function
    • How to print without a newline in Python?
    • Print Without Newline in Python 2.x
    • Using Python sys module
    • Using print() to print a list without a newline
    • Printing the star(*) pattern without newline and space

    Working of Normal print() function

    The print() function is used to display the content in the command prompt or console.

    Here is an example that shows the working of Python print without newline function.

    print("Hello World")
    print("Welcome to Guru99 Tutorials")
    

    Output

    Hello World
    Welcome to Guru99 Tutorials
    

    In the given output, you can see that the strings that we have given in print() are displayed on separate lines. The string “Hello World ” is printed first, and “Welcome to Guru99 Tutorials” is printed on the next line.

    From Python 3+, there is an additional parameter introduced for print() called end=. This parameter takes care of removing the newline that is added by default in print().

    In the Python 3 print without newline example below, we want the strings to print on same line in Python. To get that working just add end=”” inside print() as shown in the example below:

    print("Hello World ", end="")
    print("Welcome to Guru99 Tutorials")
    

    Output:

    Hello World Welcome to Guru99 Tutorials
    

    We are getting the output that we wanted, but there is no space between the strings. The string Hello World and Welcome to Guru99 Tutorials are printed together without any space.

    In order to add space or special character or even string to be printed, the same can be given to the end=”” argument, as shown in the example below.

    print("Hello World ", end=" ")
    print("Welcome to Guru99 Tutorials")
    

    So I here we have added one space to the end argument, for example (end=” “). Now, if you see the output, you should see a space between Hello World and Welcome to Guru99 Tutorials.

    Output:

    Hello World  Welcome to Guru99 Tutorials
    

    It’s not only space that you can give to the end argument, but you can also specify a string you wish to print between the given strings. So here is an example of it

    print("Hello World ", end="It's a nice day! ")
    print("Welcome to Guru99 Tutorials")
    

    Output:

    Hello, World It's a nice day!  Welcome to Guru99 Tutorials
    

    To get the strings to print without a newline, in python2.x you will have to add a comma (,) at the end of the print statement as shown below:

    print "Hello World ", 
    print "Welcome to Guru99 Tutorials."
    

    Output:

    Hello World  Welcome to Guru99 Tutorials
    

    Using Python sys module

    Yet another method that you can use to print without newline in Python is the built-in module called sys.

    Here is a working example that shows how to make use of the sys module to print without newline Python strings.

    To work with the sys module, first, import the module sys using the import keyword. Next, make use of the stdout.write() method available inside the sys module, to print your strings.

    import sys
    
    sys.stdout.write("Hello World ")
    sys.stdout.write("Welcome to Guru99 Tutorials")
    

    Output:

    Hello World Welcome to Guru99 Tutorials
    

    Using print() to print a list without a newline

    Consider a list of items for example: mylist = [“PHP”, JAVA”, “C++”, “C”, “PHYTHON”] and you want to print the values inside the list using for-loop. So here you can make use of print() to display the values inside the list as shown in the example below:

    mylist = ["PHP", "JAVA", "C++", "C", "PHYTHON"]
    for i in mylist:
    	print(i)
    

    Output:

    PHP
    JAVA
    C++
    C
    PHYTHON
    

    The output shows the list items, each printed one after another, on a new line. What if you want all the items on the list in the same line? For that, make use of end argument inside print() that will remove new line in Python and print all items of the list in the same line.

    mylist = ["PHP", "JAVA", "C++", "C", "PYTHON"]
    for i in mylist:
    	print(i, end=" ")
    

    Output:

    PHP JAVA C++ C PHYTHON
    

    Printing the star(*) pattern without newline and space

    The example of Python print without newline will make use of print() function to print stars(*) on the same line using for-loop.

    for i in range(0, 20):
        print('*', end="")
    

    Output:

    ********************
    

    Summary:

    • Python print() built-in function is used to print the given content inside the command prompt. The default functionality of Python print is that it adds a newline character at the end.
    • From Python 3+, there is an additional parameter introduced for print() called end=. The param end= takes care of removing the newline that is added by default in print().
    • In python2.x you can add a comma (,) at the end of the print statement that will remove newline from print Python.
    • Another method that you can use for Python print no newline is the built-in module called sys.

    Learn our next tutorial about Python Timer Function

    How do I print text on the next line?

    There are many ways to print new line in string been illustrated as below:.
    Using System. lineSeparator() method..
    Using platform-dependent newline character..
    Using System. getProperty() method..
    Using %n newline character..
    Using System. out. println() method..

    How do you print a line by line in Python?

    Using end keyword # print each statement on a new line print("Python") print("is easy to learn.") # new line print() # print both the statements on a single line print("Python", end=" ") print("is easy to learn. ")

    How do you continue a line in Python?

    Use a backslash ( \ ) as a line continuation character In Python, a backslash ( \ ) is a line continuation character. If a backslash is placed at the end of a line, it is considered that the line is continued on the next line.

    Can you strip \n in Python?

    Method 2: Use the strip() Function to Remove a Newline Character From the String in Python. The strip() method in-built function of Python is used to remove all the leading and trailing spaces from a string. Our task can be performed using strip function() in which we check for “\n” as a string in a string.