How to Print Without a Newline or Space in Python

Python is a popular and versatile programming language, widely used for a variety of tasks, from web development to scientific computing.

One of the key features of Python is its simple and intuitive syntax, which makes it easy for beginners to learn and use.

However, there are also some more advanced techniques that experienced Python developers can utilize to write more efficient and sophisticated code.

One such technique is printing without a newline or space in Python.

By default, the print function in Python adds a newline character at the end of the output, which causes the next output to appear on a new line.

Similarly, the print function adds a space character between multiple arguments, which causes them to be separated by spaces.

However, there are situations where you might want to suppress the newline and/or space characters.

For example, you might want to print multiple values on the same line, without any extra characters, or you might want to print a value without any additional formatting.

In this tutorial, we will explore how to print without a newline or space in Python.

Using the end Parameter of the Print Function

The end parameter of the print function can be used to specify a string that will be appended to the end of the output, instead of the default newline character.

By setting the end parameter to an empty string, you can prevent the print function from adding a newline character.

Here is an example:

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

Output:

HelloWorld

As you can see, the two print statements are printed on the same line, without any newline character.

Using the sep Parameter of the Print Function

The sep parameter of the print function can be used to specify a string that will be used as a separator between multiple arguments.

By setting the sep parameter to an empty string, you can prevent the print function from adding a space character between multiple arguments.

Here is an example:

print("Hello", "World", sep='')

Output:

HelloWorld

As you can see, the two arguments are printed without any separator character.

Using the flush Parameter of the Print Function

The flush parameter of the print function can be used to specify whether the output should be flushed immediately.

By setting the flush parameter to True, you can ensure that the output is displayed immediately, without any buffering.

Here is an example:

print("Hello", end='', flush=True)
print("World", flush=True)

Output:

HelloWorld

As you can see, the two print statements are printed on the same line, without any newline character, and the output is displayed immediately.

Using the sys.stdout.write Function

The sys.stdout.write function can be used to write raw output to the standard output stream.

Unlike the print function, the sys.stdout.write function does not add any newline or space characters by default.

Here is an example:

import sys

sys.stdout.write("Hello")
sys.stdout.

write("World")

Output:

HelloWorld

As you can see, the two sys.stdout.write statements are printed on the same line, without any newline character.


Conclusion

In this tutorial, we have explored different ways to print without a newline or space in Python.

By using the end, sep, and flush parameters of the print function, or by using the sys.stdout.write function, you can control the formatting of your output, and write more sophisticated and efficient code.