Python print() and input()
Objective 1.5 is the smallest in block 1 and produces one of PCEP's most reliable questions: `input()` always returns a string, so adding two of them concatenates rather than adds. Beyond that it is `print()` in detail - any number of arguments, `sep`, `end`, and the escape sequences - all of which appear in "what exactly does this output?" questions where a single space is the difference.
Language Fundamentals Guide 9 of 26 Beginner
- Python3.14.4
- OSUbuntu 26.04 LTS
- pip25.1.1
- TimeAbout 14 min
- Reviewed23 August 2026
Written against the versions above. `print` has been a function since Python 3.0, which is what makes `sep` and `end` possible. Any material writing `print "x"` without brackets is Python 2 and every keyword argument on this page is unavailable in it.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RUNNER01 | 192.168.0.27 | Ubuntu 26.04 LTS | Python 3.14.4 - the only machine this path needs | 2 Core | 4 GB | 50 GB |
Before you start
- guide 8 - you will need
int()on a string. - Nothing else.
-
print() takes any number of arguments
print()accepts zero or more values of any type, converts each withstr(), and joins them with a single space before adding a newline.That default space is the thing questions turn on.
sepchanges it.bash Example session cat > ~/py/printargs.py <<'PY'print("a", "b", "c")print("a", "b", sep="-")print("x", "y", sep="")print(1, 2.0, True, None)print()print("that blank line was an argument-less print")PYpython3 ~/py/printargs.pya b ca-bxy1 2.0 True None that blank line was an argument-less printExpected result
a b c,a-b,xy,1 2.0 True None, a blank line, then the last message.Success conditionYou can predict the exact characters
print()emits, spaces included. -
end, and why two prints share a line
endis whatprint()appends after the last argument. It defaults to"\n", and setting it to something else is how you keep output on one line.sepgoes *between* arguments;endgoes *after* all of them. Conflating the two is a question.bash Example session cat > ~/py/printend.py <<'PY'print("no newline here", end="")print(" - so this continues the line")print("A", end="|")print("B", end="|")print("C")PYpython3 ~/py/printend.pyno newline here - so this continues the lineA|B|CExpected resultTwo lines: the first joined from two prints, the second reading
A|B|C.Success conditionYou can join output across several
print()calls. -
Escape sequences
A backslash inside a string literal starts an escape. The ones the syllabus names are
\n(newline),\t(tab),\\(a literal backslash),\'and\"(a quote of the same kind that delimits the string).The last three lines make the count explicit, because "how many characters is this?" is a real question shape.
bash Example session cat > ~/py/escapes.py <<'PY'print("tab\there")print("line one\nline two")print("a quote: \" and a backslash: \\")print('single quotes need \'escaping\' too')print("escaped newline, length:", len("a\nb"))print("raw string, length:", len(r"a\nb"))print("raw string, value :", r"a\nb")PYpython3 ~/py/escapes.pytab hereline oneline twoa quote: " and a backslash: \single quotes need 'escaping' tooescaped newline, length: 3raw string, length: 4raw string, value : a\nbExpected resultA tab, a two-line string, a quote and a backslash, then lengths 3 and 4.
Success conditionYou can count the characters in a string containing escapes.
-
input() always returns a string
input()reads one line from the console, strips the trailing newline, and returns it as astr- whatever it looks like. Its optional argument is a prompt, printed with no newline and no automatic space.The script below asks for a name and an age and then reports what it actually got.
bash Example session cat > ~/py/greet.py <<'PY'name = input("Name: ")age = input("Age: ")print("age is a", type(age).__name__, "->", repr(age))print("Hello,", name, "- next year you are", int(age) + 1)PYprintf 'Ada\n7\n' | python3 ~/py/greet.pyName: Age: age is a str -> '7'Hello, Ada - next year you are 8Expected result
age is a str -> '7', then the greeting with 8.Success conditionYou can see that a numeric-looking answer is still text.
-
The trap this objective is famous for
Two inputs, added together.
2and3go in.This is the single most reliably examined question in objective 1.5, and the answer is not 5.
bash Example session cat > ~/py/addtrap.py <<'PY'a = input("first : ")b = input("second: ")print("a + b =", a + b)print("int(a) + int(b) =", int(a) + int(b))PYprintf '2\n3\n' | python3 ~/py/addtrap.pyfirst : second: a + b = 23int(a) + int(b) = 5Expected result
a + b = 23, thenint(a) + int(b) = 5.Success conditionYou can explain why the same
+gave two different answers. -
The two exceptions input() can raise
input()itself raises only one exception. The other comes from what you do with the result, and telling them apart matters.bash Example session printf 'x\n' | python3 -c 'print(int(input()) * 2)'Traceback (most recent call last): File "<string>", line 1, in <module> print(int(input()) * 2) ~~~^^^^^^^^^ValueError: invalid literal for int() with base 10: 'x'[exit 1]python3 -c 'input()' < /dev/nullTraceback (most recent call last): File "<string>", line 1, in <module> input() ~~~~~^^EOFError: EOF when reading a line[exit 1]Expected resultA
ValueErrorfromint(), then anEOFErrorfrominput()itself.Success conditionYou can attribute each failure to the right function.
-
What the exam does with this objective
Objective 1.5 is small, and its questions are predictable:
"What does
input() + input()print?" - the two strings joined."What type does
input()return?" -str, always."Reproduce the exact output of these three prints." - count the spaces and watch
end."How many characters is
"a\tb"?" - three."What does
print()with no arguments do?" - prints an empty line."What exception does
int(input())raise on bad input?" -ValueError, fromint().That completes PCEP block 1 - 18% of the exam, five objectives, six guides. guide 10 starts block 2, which is 29% on its own and the largest single area in either syllabus.
bash rm -f ~/py/printargs.py ~/py/printend.py ~/py/escapes.py ~/py/greet.py ~/py/addtrap.py && ls -A ~/pyExpected resultAn empty scratch directory.
Success conditionBlock 1 is complete.
Troubleshooting
Two numbers entered by a user are concatenated instead of added.
Why:
input()returns a string, and+concatenates strings.Fix:Convert as you read:
n = int(input()). Do it at the read, not at the arithmetic, so a bad value fails immediately.EOFError: EOF when reading a line.Why:
input()reached the end of the input stream - Ctrl-D, a redirect from an empty file, or a script run with no stdin.Fix:Expected when input is piped. In code that must survive it, catch
EOFErrorand use a default.Output has no space where you expected one, or a space where you did not.
Why:
print()inserts one space between arguments and nothing aroundsep/end.Fix:Use
sepdeliberately, or build the string yourself with+or an f-string when the spacing has to be exact.SyntaxWarning: invalid escape sequence.Why: A backslash followed by a character that is not a recognised escape, such as
\din a path or a regex.Fix:Use a raw string:
r"\d". See guide 3 - this became a warning in 3.12 and will eventually be an error.A prompt appears on the same line as the previous output, or not at all.
Why: The
input()prompt is written without a newline and is not flushed by every environment the same way.Fix:Normal in a terminal. When output is piped or redirected, prompts can appear bunched together - as they do in this guide's transcripts - because nothing echoes the typed reply between them.