How to print in one line dynamically with Python?

Estimated read time 1 min read

To print dynamically in one line with Python, you can use the end parameter of the print() function. By default, the end parameter is set to "\n", which adds a newline character at the end of each print statement. However, you can change the end parameter to an empty string "" to avoid the newline character and keep printing on the same line. Here’s an example:

import time

for i in range(10):
    print(i, end=" ")
    time.sleep(1)

In this example, the for loop iterates from 0 to 9. The print() function is called inside the loop with the end parameter set to a space character " ". This causes each number to be printed on the same line, separated by a space. The time.sleep(1) is used to introduce a one-second delay between each print statement.

As a result, the numbers will be printed dynamically in one line, with a space separating each number.

You can modify the code according to your requirements and formatting needs.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply