How can I python print without newline or space?

How can I python print without newline or space?

Consider the following examples using print in Python:

In both cases, a newline or space is added between each value. How can I avoid this so that the output is … instead? In other words, how can I “append” strings to the standard output stream without adding a newline or space?

Hey Neha,

You can use print with end argument: You can use the end parameter of the print function to specify what to print at the end of each line instead of the default newline character. By setting it to an empty string (“”), you can print characters consecutively without adding a newline or space.

for i in range(4):
    print('.', end='')  # This will print without adding a newline

To adding what @mark-mazay shared @nehagupta.1798 you can also use sys.stdout.write allows you to write to the standard output without automatically adding a newline character, unlike print. This is useful if you want full control over the output formatting.

import sys
for i in range(4):
    sys.stdout.write('.')  # No newline or space will be added

Using join with a list comprehension: If you have a sequence of characters that you want to print without a space or newline, you can use join to concatenate them into a single string and then print it.

print(''.join(['.' for _ in range(4)])) # Concatenates and prints all dots without spaces or newlines