Home >Backend Development >Python Tutorial >How to Print Multiple Items Sequentially on the Same Line in Python?
Printing Multiple Items Sequentially on the Same Line in Python
In Python, you can print multiple objects on the same line by separating them with commas and specifying an empty string as the end parameter in the print() function.
Referencing the provided example:
def install_xxx(): print("Installing XXX... ", end="", flush=True) install_xxx() print("[DONE]")
The end parameter controls the character that is appended to the output. In this case, we set it to an empty string to suppress the default new line character. The flush=True argument ensures that the output is displayed immediately, even if other print() statements are pending.
Python 2 Solution
Python 2 handles this scenario slightly differently. Instead of an end parameter, you can add a comma to the end of the print() line, as seen here:
def install_xxx(): print "Installing XXX... ", install_xxx() print "[DONE]"
Note that this approach will result in an extra space at the end of the output.
By utilizing either of these techniques, you can effectively print multiple items on the same line, providing a more concise and visually appealing output.
The above is the detailed content of How to Print Multiple Items Sequentially on the Same Line in Python?. For more information, please follow other related articles on the PHP Chinese website!