Python: Print No Newline Better

Python: Print No Newline Better

Alternatively, you can use the sys.stdout.write() method to print without a newline.

sentence = "" for word in ["Python", "is", "fun"]: print(word, end=" ") sentence += word + " " print("\nFinal:", sentence)

By default, print() ends with a newline ( \n ). Changing this to an empty string ( "" ) or a space ( " " ) keeps the output on the same line. Method 1: The end Parameter (Recommended) This is the standard approach for Python 3.x.

print("A", end=""); print("B") # AB

Use the end parameter in print() to control what is printed at the end instead of \n .

Output: apple | banana | cherry |

Output:

Output: Hello, World!

print("Hello", end="") print("World")

print("Loading", end="...") print("Done") python print no newline

print("Hello, ", end='') print("World!")

| Version | Without newline | |---------|----------------| | Python 3 | print("text", end="") | | Python 2 | print "text", (trailing comma) |