Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»General Articles»How to Display Output in the Same Line in Python

    How to Display Output in the Same Line in Python

    By RahulFebruary 6, 20233 Mins Read

    When writing a Python script, there may be instances where you want to display output in the same line instead of having it on a new line every time. This can be particularly useful when creating a progress bar or updating output in real time.

    Advertisement

    In this tutorial, we’ll cover the methods you can use to display output in the same line in Python.

    Method 1: Using the end Argument in the print() Function

    The print() function in Python allows you to specify the end character that follows the output. By default, this is set to a newline character \n, which causes the output to display on a new line. To keep the output in the same line, we can specify a different end character such as a space .

    Here’s an example:

    1
    2
    print("Hello,", end=' ')
    print("how are you?")

    The output of this code will be:

    Output:
    Hello, how are you?

    As you can see, the two outputs are displayed in the same line.

    Method 2: Using the flush Argument in the print() Function

    Another way to display output in the same line is to use the flush argument in the print() function. This argument tells Python to flush the output buffer, which means that the output is displayed immediately instead of being stored in a buffer and displayed later. To use this argument, you’ll need to set it to True.

    Here’s an example:

    1
    2
    3
    import sys
    print("Hello,", end=' ', flush=True)
    print("how are you?", flush=True)

    The output of this code will be the same as in Method 1, but the advantage of using the flush argument is that it will work regardless of the output buffer size.

    Method 3: Using the sys.stdout.write() Method

    Another way to display output in the same line is to use the sys.stdout.write() method. This method writes to the standard output stream (sys.stdout) without adding a newline character at the end.

    Here’s an example:

    1
    2
    3
    import sys
    sys.stdout.write("Hello, ")
    sys.stdout.write("how are you?")

    The output of this code will be:

    Output:
    Hello, how are you?

    As you can see, the two outputs are displayed in the same line.

    Method 4: Using Carriage Return (\r)

    The fourth method to display output in the same line is to use a carriage return (\r). A carriage return is a special character that moves the cursor to the beginning of the current line and replaces the existing content with new content.

    Here’s an example:

    1
    2
    3
    4
    import time
    for i in range(10):
        print("\r Progress: {}%".format(i*10), end='')
        time.sleep(1)

    The output of this code will be:

    Output:
    Progress: 90%

    As you can see, the progress bar is displayed in the same line and updates every second.

    Conclusion

    In this tutorial, we covered four methods to display output in the same line in Python. You can choose the method that best suits your needs based on the type of output and the level of control you need over the output. Whether you’re creating a progress bar or just want to update output in real time, these methods will help you achieve your desired result.

    It’s important to note that the sys.stdout.write() method and the \r character are the most direct methods for controlling the output in the same line, as they allow you to write directly to the standard output stream. On the other hand, the end argument in the print() function and the flush argument are simpler methods that may be easier to use in some cases.

    In conclusion, with these methods, you can now easily control the display of output in Python, whether you’re a beginner or an expert. Try these methods out in your next Python project and see the results for yourself!

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Install PHP 8.2-7.4 on RHEL & CentOS Stream 9

    How to Install MySQL 8.0 on RHEL & CentOS Stream 9

    How to Split Large Archives in Linux using the Command Line

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • What is the /etc/mtab File in Linux
    • The “Hello World” Challenge: Printing in 20 Different Programming Languages
    • How to Install PHP 8.2-7.4 on RHEL & CentOS Stream 9
    • How to Install MySQL 8.0 on RHEL & CentOS Stream 9
    • How to Split Large Archives in Linux using the Command Line
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.