Python - Print without New Line at the End


Python - Print Without New Line at the End

By default, the print() function in Python adds a new line after each output. However, you can modify this behavior by using the end parameter to specify what should be printed at the end of the output. To print without a new line, set end to an empty string or any custom character of your choice.


Syntax

print(object, ..., end=' ')

Where:

  • object: The data to be printed.
  • end: Defines what is appended at the end of the print statement. The default value is '\n' (new line).

Setting end='' prevents the new line from being added.


Examples

1. Print Without New Line

In this example, we demonstrate how to print multiple strings without new lines between them.

Python Program

# Print without new line
print('Hello', end='')
print(' World!', end='')
print(' Welcome to', end='')
print(' PythonExamples.org')

Output

Hello World! Welcome to PythonExamples.org

2. Custom End Character

You can replace the default new line with a custom character or string using the end parameter. For example, you can add a hyphen, space, or other symbols instead of a new line.

Python Program

# Print with custom end character
print('Hello', end='-')
print('World', end='+')
print('Python', end='###')
print('Examples', end='\n\n')  # Adds two new lines

Output

Hello-World+Python###Examples

3. Print in a Loop Without New Lines

This example demonstrates printing numbers in a loop without new lines, separated by commas.

Python Program

# Print numbers in a loop
for i in range(1, 6):
    print(i, end=', ')
print('Done!')  # Final print with default behavior

Output

1, 2, 3, 4, 5, Done!

4. Append Data to the Same Line

This example demonstrates appending strings or data to the same line incrementally.

Python Program

# Append data to the same line
print('Loading', end='')
for _ in range(3):
    print('.', end='')
print(' Done!')

Output

Loading... Done!

Use Cases

Printing without new lines is useful in scenarios like:

  • Displaying progress updates or animations.
  • Appending data to the same line dynamically.
  • Custom formatting of output for clarity.

Summary

In this tutorial, we explored how to print without a new line in Python using the end parameter of the print() function. By customizing the end value, you can control how the output is formatted. Experiment with the examples provided to master this technique for your Python projects.


Python Libraries