Online Tutorials & Training Materials | STechies.com
Register Login

Python: How to Print Without Newline?

24 Dec 2018 7:41 am || 0

Python is among the extremely simple computer programming languages that are a favorite among various beginner programmers. It is a general-purpose programming language that comes with a design philosophy of easy readability. With a basic knowledge of programming, anyone can master Python in a matter of a few months.

The Problem

Printing the output is probably the most common command in any programming language and among the first commands that you lean. Similarly, it is among the first commands you will learn in Python.

However, unlike other programming languages that print several outputs in the same line without a new line command, Python by default prints every output on a different line.

For Example

print("Good Morning!")
print("What a wonderful day!")

Output

Good Morning!
What a wonderful day!

In order to print different outputs in the same line, you have to introduce certain changes in the print command.

Print Without Newline in Following Ways

Print Without Newline in Python

    1. 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. However, you can avoid this by introducing the argument end and assigning an empty string to it. This will prevent the next output from being printed in a new line.

    # use the argument "end" to specify the end of line string
    
    print("Good Morning!", end = '')
    print("What a wonderful day!")

    Output:

    Good Morning!What a wonderful day!
    

    2. Using "sys" Library (Python 3.X)

    #Import the inbuilt sys library
    import sys
    #First Line of output
    sys.stdout.write("Hey! Welcome to the STechies.")
    #Second Line of output
    sys.stdout.write("We have the best python tutorial")

    Output:

    Hey! Welcome to the STechies.We have the best python tutorial
    

    Code explanation:
    The sys module in Python contains many functions and variables to work on the runtime environment. The information about functions, constants and methods of the Python interpreter are present in this module.

    The sys.stdout is a file object that is used for the displaying the output of a print statement. The sys.stdout.write() method does not add a newline at the end of a string to be displayed.

    In the code you can see that both strings are printed on the same line without any spaces in between.

    Note: The output of both the example above will be in one line without adding space in between the strings.

    Print Output with Space

    Now if you want to insert a space in your output, you have to use a space at the end of your first print function.

    Input

    # use the argument "end" to specify the end of line string and print with space
    
    print("Good Morning! ", end = '')
    print("What a wonderful day!")

    Output

    Good Morning! What a wonderful day!
    

    Printing a "List" With "for" Loop Without New Line

    Code Example:

    # Python program to print list without new line character
    
    # Declare a List
    listfruits = ['Apple', 'Banana', 'Orange', 'Mango']
    
    # Print list using for Loop
    for fruit in listfruits:
        print(fruit, end='')
    

    Output:

    AppleBananaOrangeMango

    Explanation:
    In the program written above, the elements of a list are stored in a variable called listfruits. A for loop is executed to iterate over the elements and print them out one by one. You can see that an end argument is passed along with the print statement. This avoids the strings to be printed on different lines.

    You can see that the elements of the list are printed together on the same line without any spaces.

    Printing With a Space Between List Items

    Code Example:

    # Python program to print list without new line character
    
    # Declare a List
    listfruits = ['Apple', 'Banana', 'Orange', 'Mango']
    
    # Print list using for Loop
    for fruit in listfruits:
        print(fruit, end=' ')
    
    

    Output:

    Apple Banana Orange Mango

    Explanation:
    Here, a for loop iterated over list items to print them out on the same line using the print statement. This is achieved by passing the end argument that contains the value of an empty string. There is a space between the single quotes, as observed in the code. This creates a space between the list elements when printed out on a single line.  

    Printing Without A Newline In Python 2.X

    To print output in Python 2.X, you have to input print in the form of a statement. However, in this version of Python, every output was printed in the next line.

    Unlike Python 3.X, Python 2.X does not have the 'end' argument.

    3. Using "comma" to Terminate Print Statement

    In order to print in the same line in Python 2.X, all you have to do is terminate your print statement with a comma.

    Input

    # print without a newline but with space
    # terminate print statement with a comma
    
    print ("Good Morning!"),
    print ("What a wonderful day!")

    Output

    Good Morning! What a wonderful day!

    Python Print Without Newline Video Tutorial

    Conclusion

    With most other programming languages requiring the position of a new line being mentioned in the program, many first time users may find printing with Python a little inconvenient. However, it is actually time-saving as it automatically detects a new line in the output with every print function or statement.

    Whenever you need an output printed in the same line, all you need to do is to terminate your print function with the argument 'end' in Python3 or introduce a comma in Python 2.This simple addition will help you get rid of your new line woes!