QA Graphic

Creative Ways to Display Information with Python’s print Statement

The print() function is one of the most fundamental and frequently used functions in Python. While it's typically used to display output in a straightforward manner, you can get creative with it to format and present information in a more engaging way. In this post, we'll explore a fun and practical technique to enhance the way information is displayed in Python.


Printing Inline with end=

By default, print() adds a newline after each output. However, you can change this behavior using the end= parameter.

import random

# Print numbers on the same line, separated by commas
for i in range(5):
    print(random.randint(1, 99), end=", " if i < 4 else "n")

# Print numbers on the same line, separated by spaces
for i in range(5):
    print(random.randint(1, 99), end=" ")

print("nDone!")  # Moves to a new line after the loop

Why this is useful?

  • It helps create compact and clean output.
  • You control when to insert a new line or separator dynamically.