Python Interview Question

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

How to Print in the Same Line in Python

Python’s print() function is one of the first functions beginners learn. It is used to display text, numbers, variables, and other information on the screen. By default, every print() statement moves the cursor to a new line after displaying its output.

Sometimes, however, you may want to print multiple values on the same line in Python. This is common when displaying numbers, processing lists with loops, creating formatted output, or showing information in a compact way.

Python provides a simple solution for this through the end parameter of the print() function. In this tutorial, we will understand the default behavior of print() and learn how to use end to print output on the same line.

How to Print in the Same Line in Python

Complete Advance AI Topics: Click Here

Complete Python Course with Advance Topics: Click Here

Default Behavior of print() in Python

By default, the print() function adds a newline after displaying the output. Therefore, when you use multiple print() statements, each output appears on a separate line.

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

Output:

Welcome
To
UpdateGadh

Here, Python moves to the next line after every print() statement. This happens because the default value of the end parameter is a newline character.

If you want all the text on one line and do not need separate print() statements, you can simply write:

print("Welcome To UpdateGadh")

Output:

Welcome To UpdateGadh

But if you need multiple print() calls, the end parameter is more useful.

Using the end Parameter to Print on the Same Line

The end parameter tells Python what should be printed after the value. Normally, print() uses a newline. By changing end, you can keep the next output on the same line.

For example:

print("Welcome", end=" ")
print("To", end=" ")
print("UpdateGadh")

Output:

Welcome To UpdateGadh

In this example, end=" " adds a space instead of moving to a new line. As a result, the next print() statement continues on the same line.

One of the most common uses of the end parameter is printing list elements on the same line.

numbers = [10, 11, 12, 13, 14, 15]

for number in numbers:
    print(number, end=" ")

Output:

10 11 12 13 14 15

Without end=" ", every number would appear on a separate line. The space after each number makes the output easier to read.

If you want to print values directly beside each other without spaces, use an empty string with the end parameter.

for number in [1, 2, 3, 4, 5]:
    print(number, end="")

Output:

12345

This method is useful when you want complete control over how the output is displayed.

Using a Custom Separator with end

The end parameter is not limited to spaces or empty strings. You can use almost any character or text as the ending value.

For example, you can use an ampersand:

numbers = [10, 11, 12, 13, 14, 15]

for number in numbers:
    print(number, end="&")

Output:

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

You can also use a comma:

for number in [1, 2, 3, 4]:
    print(number, end=", ")

Output:

1, 2, 3, 4, 

This makes end useful when you need custom output formatting.

You can also use end when you have only two or a few print() statements.

print("Python", end=" ")
print("Programming")

Output:

Python Programming

Another example is:

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

Output:

Hello World

The important point is that the first print() does not create a new line because its end value is a space.

How the end Parameter Works

The syntax of the print() function can be written as:

print(value, end="...")

The value is the information you want to display, while end determines what Python should print after that value.

CodeResult
print("A")Prints A and moves to a new line
print("A", end=" ")Prints A followed by a space
print("A", end="")Prints A without adding anything
print("A", end="&")Prints A followed by &

Why Use the end Parameter?

The end parameter gives you better control over console output. Some common uses include:

  • Printing multiple values on the same line.
  • Displaying list elements in a single line.
  • Removing unwanted spaces or newlines.
  • Adding custom separators between outputs.
  • Formatting output in loops.
  • Creating simple progress-style console output.

Example with a for Loop

Here is another simple example that prints numbers from 1 to 10 on the same line:

for i in range(1, 11):
    print(i, end=" ")

Output:

1 2 3 4 5 6 7 8 9 10

This is a common pattern when working with loops and console-based Python programs.

YT:- DecodeIT

How to Print on a New Line

If you want Python to return to its normal behavior, simply use print() without changing the end parameter.

print("Hello")
print("Python")

Output:

Hello
Python

You can also explicitly use the newline character:

print("Hello", end="\n")
print("Python")

Here, \n represents a new line.

Conclusion

Printing multiple values on the same line in Python is easy when you understand the end parameter of the print() function. By default, Python adds a newline after every print statement, but you can replace that behavior with a space, empty string, or any custom character.

For loops, end=" " is especially useful for displaying numbers or list elements in a single line. You can also use end="" when values need to appear directly next to each other.

Understanding end will help you create cleaner and more controlled output in your Python programs.

Frequently Asked Questions (FAQ)

1. How do I print in the same line in Python?

Use the end parameter, such as print("Hello", end=" "), to keep the next output on the same line.

2. How do I print numbers on the same line using a for loop?

Use print(number, end=" ") inside the loop to display numbers on the same line.

3. How can I print without a space in Python?

Use end="". For example, print(1, end="") prevents Python from adding a space or newline after the value.

4. What is the default value of end in print()?

The default value of end is a newline character, so the next output appears on a new line.

5. Can I use special characters with end?

Yes. You can use spaces, commas, symbols, or other strings as the value of end.

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

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us