Lesson 07
print() with end=
Keep print() output on the same line by passing end with a space, arrow, or comma delimiter.
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.
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)
# PrintDefaultEx.py
for val in range(10, 13):
print(val)10
11
12Each 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.
# PrintEndSpaceEx.py
for val in range(10, 16):
print(val, end=" ")10 11 12 13 14 15The space becomes the separator between each value.
Arrow delimiter (PrintEndArrowEx.py)
You can use any string as the delimiter.
# PrintEndArrowEx.py
for val in range(10, 16):
print(val, end="-->")10-->11-->12-->13-->14-->15-->This is useful when displaying sequences or process flows.
Comma delimiter (PrintEndCommaEx.py)
# PrintEndCommaEx.py
for val in range(10, 16):
print(val, end=",")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:
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)
# PrintEndRollEx.py
for roll in range(101, 106):
print(roll, end=" ")101 102 103 104 105Process steps (PrintEndStepsEx.py)
# PrintEndStepsEx.py
steps = ["Start", "Load", "Process", "Finish"]
for step in steps:
print(step, end=" -> ")Start -> Load -> Process -> Finish ->CSV-style output (PrintEndCsvEx.py)
# PrintEndCsvEx.py
marks = [85, 92, 78, 88]
for mark in marks:
print(mark, end=",")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):
# PrintNoEndEx.py
print(1)
print(2)
print(3)1
2
3With end= (PrintWithEndEx.py):
# PrintWithEndEx.py
print(1, end=" ")
print(2, end=" ")
print(3)1 2 3The 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 delimiterend=" "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
-
What does
end=replace?
The default newline. -
What is the output of
print(5, end=" ")followed byprint(6)?
5 6 -
What delimiter creates arrow-separated output?
end="-->" -
Does
range(10, 16)include 16?
No, it stops at 15.
Next lesson: reading input from the keyboard using input().