Skip to content
ComputerCS
Start learning

Python Programming · Lesson 2.2.1.4

print(), input() & Escape Sequences

Talking to the user: print with sep and end, prompts that make input() friendly, and the backslash codes \n and \t that shape output.

01

Learning objectives

  • Print multiple values in one call and control the separator with sep.
  • Suppress or replace the automatic newline using end.
  • Write input() with a clear prompt and cast the result in the same line.
  • Use the escape sequences \n, \t, \\ and \" inside strings.
  • Format a small receipt/table of output using \n and \t.
02

Lesson notes

print() is the first function every Python programmer meets, and it is more configurable than it looks. Pass several values separated by commas and print outputs them in order with a single space between each — print("Marks:", 88) gives Marks: 88. The space is just the default value of the sep (separator) parameter: print("A", "B", sep="-") gives A-B, and sep="" glues values with nothing between them. sep answers 'what goes BETWEEN my values?' and is a favourite 2-mark question.

The other default worth mastering: print finishes each call by moving to a new line. That newline is the value of the end parameter — by default end="\n". Override it and consecutive prints share a line: print(1, end=" "); print(2) outputs 1 2 instead of stacking them. Building progress bars, rows of a pattern and comma-separated lines all use end; the pattern-printing challenges in the Labs lean on it heavily.

input() pauses the program and waits for the user to type a line and press Enter. Its one optional argument is the prompt — the text shown to the user before they type: name = input("Your name: "). Without a prompt the program sits there silently, which feels broken to the person using it, so professional code always prompts. Remember lesson 2.2b: whatever arrives comes back as a str, so numbers arrive pre-quoted and the cast belongs on the same line: age = int(input("Age: ")).

Some characters cannot be typed directly into a string — a newline, a tab, a quote that would end the string early. Escape sequences solve this: a backslash tells Python 'the next character is special'. The working set: \n newline, \t tab, \\ a literal backslash, \" a double quote inside a double-quoted string, and \' similarly for single quotes. print("Name:\tAyesha\nRoll:\t41") prints two tab-aligned lines. Escape sequences are how one print can lay out a whole receipt.

Output design is examinable: 'Write a program that reads two numbers and prints their sum, formatted as ...' appears in every past paper. The recipe is always the same — prompt, read, cast, compute, print with a labelled message. Keep labels human (Total = 50, not 50), align columns with \t, and build multi-line blocks with \n before resorting to extra print calls. The next lessons give you f-strings, which make all of this even cleaner — but sep, end and escapes remain the fundamentals underneath.

03

Worked code examples

sep and end — the two dials of print

Default sep is one space and default end is a newline. sep="-" replaces the between-values text; end="... " replaces the newline, so the next print continues on the same line.

print("A", "B", "C")            # default sep and end
print("A", "B", "C", sep="-")   # custom separator
print("Loading", end="... ")    # no newline
print("done")                   # continues the line
Output
A B C
A-B-C
Loading... done

Input, cast in one line

The program is fed Ayesha, then 17. In a real terminal you would add a prompt inside input() — input("Your name: ") — so the user knows what to type. age is cast with int() immediately, so age + 1 does real arithmetic: 18, not "171".

name = input()
age = int(input())
print("Salam,", name + "!")
print("Next year you will be", age + 1)
Output
Salam, Ayesha!
Next year you will be 18

Escape sequences build a formatted receipt

\t jumps to the next tab stop, aligning the columns; \n (not needed here — print adds one) would break lines; \" places quote marks inside the double-quoted string.

print("Item\tQty\tPrice")
print("Pen\t4\t50.0")
print("Total\t\t" + str(4 * 12.5))
print("She said: \"Bring the receipt\"")
Output
Item	Qty	Price
Pen	4	50.0
Total		50.0
She said: "Bring the receipt"
04

Key terminology

prompt

The message printed by input() before waiting, telling the user what to type.

sep

print's separator parameter — the text placed between values (default: one space).

end

print's terminator parameter — the text placed after the last value (default: a newline).

escape sequence

A backslash pair (\n, \t, \\, \") representing a character that cannot be typed directly.

newline

The invisible character that moves output to the next line; \n inside strings, print's default end.

05

Common misconceptions

Common mistake

print() always starts a fresh line before printing.

Correct understanding

The newline comes AFTER each call (that is end="\n"). print(1, end="") leaves the line open for the next print.

Common mistake

input() can return an int when the user types digits.

Correct understanding

input() always returns a str. "17" is text until you cast it: int(input(...)).

Common mistake

\n prints the two characters backslash and n.

Correct understanding

Inside a string literal, \n is the newline character. To print a literal backslash-n you would write "\\n".

06

Check your understanding

1. What does print("x", "y", sep="*") output?

2. Which line keeps the next print() on the SAME line?

3. The user types 25 at the prompt. What is returned by input("Age: ")?

NERDLYY · FEEDBACK

Help shape Nerdlyy

Your feedback goes directly to the team. We read every submission.

No reply needed — you can send feedback without sharing contact details.