In this Python tutorial, we will discuss how to print without a newline in Python. Also we will different ways to print without newline in loop in Python.
When learning Python, one of the first things you learn is how to use the print() function to output data to the console. By default, the print() function appends a newline character at the end of the output. However, there might be situations where you want to print without a newline, let us see different ways to print in the same line in Python.
The print() function in Python
The print() function in Python is used to output data to the console. By default, it appends a newline character (‘\n’) at the end of the output, which causes the next printed output to appear on a new line. Here’s an example:
print("Hello, World!")
print("Welcome to Python.")
Output:
Hello, World!
Welcome to Python.
Print Without a Newline in Python using the ‘end’ parameter
You can use the ‘end’ parameter within the Python print() function to change the default behavior of appending a newline character at the end of the output. The ‘end’ parameter allows you to specify a different string to be appended at the end of the output.
Example:
print("Hello, World!", end="")
print("Welcome to Python.")
Output:
Hello, World!Welcome to Python.
Python print without newline using the ‘sep’ parameter
The ‘sep’ parameter can be used to print multiple arguments without a newline in between in Python. By default, the ‘sep’ parameter has a space character (‘ ‘) as its value. You can change this value to an empty string to remove the space between printed arguments.
Example:
print("Hello, World!", "Welcome to Python.", sep="")
Output:
Hello, World!Welcome to Python.
Python print without newline using the sys.stdout.write() method
Another way to print without a newline in Python is by using the write() method of the sys.stdout object. This method writes a string to the console without appending a newline character.
Example:
import sys
sys.stdout.write("Hello, World!")
sys.stdout.write("Welcome to Python.")
Output:
Hello, World!Welcome to Python.
Python print without newline in loop
Now, let us see a few ways to print without a newline in a loop in Python.
In Python, when printing inside a loop, the default behavior is to append a newline character at the end of each printed output. However, there might be situations where you want to print without a newline while iterating through a loop. Here, we will discuss how to print without a newline in a loop using various methods.
Using the ‘end’ parameter in a loop
The ‘end’ parameter within the print() function can be used to change the default behavior of appending a newline character at the end of the output. When printing inside a loop, you can use the ‘end’ parameter to control the output format.
Example-1: Print numbers from 1 to 5 in a single line
for i in range(1, 6):
print(i, end=" ")
Output:
1 2 3 4 5
Example 2: Print a list of names separated by a comma
names = ["Alice", "Bob", "Charlie", "David"]
for name in names[:-1]:
print(name, end=", ")
print(names[-1])
Alice, Bob, Charlie, David
Using the ‘sep’ parameter in a loop
The ‘sep’ parameter in the print() function allows you to control the separator between multiple arguments. When printing inside a loop, you can use the ‘sep’ parameter to achieve the desired output format.
Example: Print elements of a list separated by a comma
names = ["Alice", "Bob", "Charlie", "David"]
print(*names, sep=", ")
Output:
Alice, Bob, Charlie, David
Using sys.stdout.write() in a loop
Another way to print without a newline in a loop is by using the write() method of the sys.stdout object. This method writes a string to the console without appending a newline character.
Example: Print numbers from 1 to 5 in a single line
import sys
for i in range(1, 6):
sys.stdout.write(str(i) + " ")
Output:
1 2 3 4 5
We have learned different methods to print without a newline in a loop in Python. We covered how to use the ‘end’ and ‘sep’ parameters of the print() function, as well as the sys.stdout.write() method.
Conclusion
We have also learned different methods to print without a newline in Python. We covered how to use the ‘end’ and ‘sep’ parameters of the print() function, as well as the sys.stdout.write() method.
You may also like:
- Python program to print pattern
- How to print factorial of a number in Python
- Python program to print prime numbers
- Python program to print element in an array
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.