Menu Close

How to print everything in one line dynamically with Python?

Sometimes, we want to print everything in one line dynamically with Python.

In this article, we’ll look at how to print everything in one line dynamically with Python.

How to print everything in one line dynamically with Python?

To print everything in one line dynamically with Python, we can set the end parameter to an empty string and the sep parameter to a string with one space.

And we set flush to True.

For instance, we write:

for item in range(1, 10):
    print(item, sep=' ', end='', flush=True)

We loop through the iterator that returns 1 to 9.

Then we call print in the loop body with the options set.

We replaced new line with an empty string for the end character for each print call.

Now we should see:

123456789

printed on the screen.

Conclusion

To print everything in one line dynamically with Python, we can set the end parameter to an empty string and the sep parameter to a string with one space.

And we set flush to True.

Posted in Python, Python Answers