Home >Backend Development >Python Tutorial >How to Print Multiple Elements on the Same Line in Python?

How to Print Multiple Elements on the Same Line in Python?

DDD
DDDOriginal
2024-12-12 14:33:10456browse

How to Print Multiple Elements on the Same Line in Python?

How to Print Multiple Elements on the Same Line in Python

In Python, printing multiple elements on the same line can be achieved through modification of the print() function's end parameter.

Python 3 Solution

In Python 3, the print() function's end parameter takes a default value of "n" (new line). Modifying this parameter to an empty string prevents the insertion of a new line at the end of the line.

def install_xxx():
    print("Installing XXX...      ", end="", flush=True)

install_xxx()
print("[DONE]")

Python 2 Solution

In Python 2, placing a comma at the end of the print() line suppresses the insertion of a new line (note that an additional space will appear at the end of the output).

def install_xxx():
   print "Installing XXX...      ",

install_xxx()
print "[DONE]"

By adjusting the end parameter in Python or using commas in Python 2, you can effectively print multiple elements on the same line in your script, allowing you to create output in the desired format.

The above is the detailed content of How to Print Multiple Elements on the Same Line in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn