Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 07

print() with end=

Keep print() output on the same line by passing end with a space, arrow, or comma delimiter.

Beginner10 min

What you will learn

By the end of this lesson, you'll understand:

  • What is Syntax-6: print(value, end="Delimiter")?
  • Why does print() normally move to the next line?
  • How do you display values horizontally on the same line?

What is end=?

By default, every print() statement ends with a newline (\n), so the next output starts on the next line.

The end= parameter replaces the newline with any delimiter you choose.

python
print(value, end="Delimiter")

Key idea: instead of moving to the next line, Python prints the delimiter and continues on the same line.

Default behavior of print() (PrintDefaultEx.py)

python
# PrintDefaultEx.py
for val in range(10, 13):
    print(val)
text
10
11
12
Default print() stacks 10, 11, 12 on new lines

Each call to print() automatically moves the cursor to the next line.

Syntax-6: space delimiter (PrintEndSpaceEx.py)

Use a space to print values horizontally. This is the first example from your Display Result notes.

python
# PrintEndSpaceEx.py
for val in range(10, 16):
    print(val, end=" ")
text
10 11 12 13 14 15
end=" " keeps 10 11 12 13 on one line

The space becomes the separator between each value.

Arrow delimiter (PrintEndArrowEx.py)

You can use any string as the delimiter.

python
# PrintEndArrowEx.py
for val in range(10, 16):
    print(val, end="-->")
text
10-->11-->12-->13-->14-->15-->
Arrow delimiter between 10, 11, 12

This is useful when displaying sequences or process flows.

Comma delimiter (PrintEndCommaEx.py)

python
# PrintEndCommaEx.py
for val in range(10, 16):
    print(val, end=",")
text
10,11,12,13,14,15,

The comma replaces the newline after every value.

Understanding range(10, 16)

Your notes use range(10, 16). It generates:

range(10, 16) produces 10 through 15

Rule: range(start, stop) includes the start value but excludes the stop value.

Expression Output
range(1, 5) 1, 2, 3, 4
range(10, 16) 10, 11, 12, 13, 14, 15

Default vs end=

Code Result
print(val) Each value appears on a new line
print(val, end=" ") Values stay on the same line with spaces
print(val, end="-->") Values separated by arrows
print(val, end=",") Values separated by commas

Real-world examples

Roll numbers (PrintEndRollEx.py)

python
# PrintEndRollEx.py
for roll in range(101, 106):
    print(roll, end=" ")
text
101 102 103 104 105

Process steps (PrintEndStepsEx.py)

python
# PrintEndStepsEx.py
steps = ["Start", "Load", "Process", "Finish"]
for step in steps:
    print(step, end=" -> ")
text
Start -> Load -> Process -> Finish ->

CSV-style output (PrintEndCsvEx.py)

python
# PrintEndCsvEx.py
marks = [85, 92, 78, 88]
for mark in marks:
    print(mark, end=",")
text
85,92,78,88,

This is similar to comma-separated values (CSV).

Why use end=?

Without end=, creating horizontal output requires more work.

Without end= (PrintNoEndEx.py):

python
# PrintNoEndEx.py
print(1)
print(2)
print(3)
text
1
2
3

With end= (PrintWithEndEx.py):

python
# PrintWithEndEx.py
print(1, end=" ")
print(2, end=" ")
print(3)
text
1 2 3

The final print(3) uses the default newline, ending the line neatly.

Key terms

Term Meaning
end= Replaces the default newline character
Delimiter The string printed after each value
Horizontal output Output displayed on the same line
Newline (\n) Default ending used by print()
range() Generates a sequence of numbers

Summary

  • By default, print() ends with a newline
  • end="..." replaces that newline with any delimiter
  • end=" " prints values horizontally with spaces (PrintEndSpaceEx.py)
  • end="-->" creates arrow-separated output (PrintEndArrowEx.py)
  • range(10, 16) produces 10 to 15, because the stop value is excluded

Quick knowledge check

  1. What does end= replace?
    The default newline.

  2. What is the output of print(5, end=" ") followed by print(6)?
    5 6

  3. What delimiter creates arrow-separated output?
    end="-->"

  4. Does range(10, 16) include 16?
    No, it stops at 15.

Next lesson: reading input from the keyboard using input().