How to Print in the Same Line in Python

How to Print in the Same Line in Python

How to Print in the Same Line in Python

Python’s print() function is a fundamental tool for displaying output. By default, this function appends a newline after each print statement, causing the subsequent output to appear on a new line. However, Python provides mechanisms to modify this behavior and print outputs on the same line seamlessly. To grasp this idea, let’s look at a few examples.

Complete Advance AI topics:- CLICK HERE
Complete Python Course with Advance topics:-Click here

Example 1: Default Behavior of print()

Consider the following code:

print("Welcome")  
print("To")  
print("UpdateGadh")  

Output:

Welcome  
To  
UpdateGadh  

Here, each print() call outputs a line and then moves to the next line. If you want to combine all these into a single line, you can use a single print() statement:

print("Welcome To UpdateGadh")  

Output:

Welcome To UpdateGadh  

This is straightforward, but what if we want to use multiple print() calls while keeping the output on the same line? For this, Python provides the end parameter.

Example 2: Using the end Parameter

The print() function has an optional parameter called end, which specifies what should be printed at the end of the output instead of the default newline (\n).

list1 = [10, 11, 12, 13, 14, 15]  
for i in list1:  
    print(i, end=" ")  

Output:

10 11 12 13 14 15  

The end parameter in this example is set to a single space (” “). Instead of jumping to a new line after printing each element, the program appends a space after each output.

Example 3: Customizing the end Parameter

Any character or string can be assigned to the end parameter. Here’s an illustration:

list1 = [10, 11, 12, 13, 14, 15]  
for i in list1:  
    print(i, end="&")  

Output:

10&11&12&13&14&15&  

The end argument in this code is set to &. As a result, an ampersand is appended after each element instead of a newline. This flexibility allows you to format the output as needed.

Why Use the end Parameter?

  • Compact Output: Helps in generating concise output without unnecessary newlines.
  • Custom Delimiters: Allows easy separation of values using characters like commas, spaces, or special symbols.
  • Dynamic Output: Useful in dynamic contexts like progress bars or real-time status updates in console applications.

Summary

The end parameter in Python’s print() function is a simple yet powerful tool to control how output is formatted. By leveraging this parameter, you can print multiple outputs on the same line, customize delimiters, or create dynamic display patterns.

With this approach, your Python scripts will become more elegant and versatile in handling output formatting. Keep experimenting and enhance your coding skills!


Stay tuned for more Python tutorials from UpdateGadh!

Download New Real Time Projects :-Click here

how to print in same line in python using for loop
How to Print in the Same Line in Python
python print same line overwrite
how to print in the same line in python without space
python print on same line in while loop
how to put two print statements on the same line python
how to print new line in python
how to print numbers side by side in python using for loop
how to print in the same line in java
how to print in the same line in python using for loop

Post Comment