CertGrid CertGrid
Hands-on Lab·Certified Entry-Level Python Programmer

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

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.

One machine, and any shell with Python 3 will do - these exams test the language, not a distribution.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSPython 3.14.4 - the only machine this path needs2 Core4 GB50 GB

Before you start

  1. print() takes any number of arguments

    print() accepts zero or more values of any type, converts each with str(), and joins them with a single space before adding a newline.

    That default space is the thing questions turn on. sep changes 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 print

    Expected resulta 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.

  2. end, and why two prints share a line

    end is what print() appends after the last argument. It defaults to "\n", and setting it to something else is how you keep output on one line.

    sep goes *between* arguments; end goes *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|C

    Expected resultTwo lines: the first joined from two prints, the second reading A|B|C.

    Success conditionYou can join output across several print() calls.

  3. 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\nb

    Expected 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.

  4. input() always returns a string

    input() reads one line from the console, strips the trailing newline, and returns it as a str - 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 8

    Expected resultage is a str -> '7', then the greeting with 8.

    Success conditionYou can see that a numeric-looking answer is still text.

  5. The trap this objective is famous for

    Two inputs, added together. 2 and 3 go 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) = 5

    Expected resulta + b = 23, then int(a) + int(b) = 5.

    Success conditionYou can explain why the same + gave two different answers.

  6. 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 ValueError from int(), then an EOFError from input() itself.

    Success conditionYou can attribute each failure to the right function.

  7. 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, from int().

    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 ~/py

    Expected resultAn empty scratch directory.

    Success conditionBlock 1 is complete.

Troubleshooting

Official sources