Python print() without newline

In Python, you can print without adding a newline character by using the end parameter of the print() function. By default, the print() function appends a newline character (\n) at the end of the printed content.

Print without Newline

print("Hello, ", end="") print("World!") # Output: Hello, World!

In this example, the end parameter is set to an empty string. As a result, the second print() statement doesn't add a newline character after "Hello, ".

Print Multiple Items without Newline

print("Python", "is", "fun", sep="-", end=".\n") # Output: Python-is-fun.

The sep parameter specifies the separator between items in the print() function. The end parameter is set to ".\n", which results in a period and a newline after the printed content.

Print in a Loop without Newline

numbers = [1, 2, 3, 4, 5] for num in numbers: print(num, end=" ") # Output: 1 2 3 4 5

In this loop example, the end parameter is set to a space character, ensuring that the numbers are printed with spaces between them.

Conclusion

Using the end parameter allows you to customize the behavior of the print() function, controlling whether a newline character is appended or not. This is useful when you want to format output precisely or display items on the same line.