Vinay Garg Assistant Professor

Lesson 27: Sending output with print() and reading input with input()

print() writes to STDOUT — the screen or a terminal — and input() reads a line of text from the keyboard. Everything you already know about strings applies to what input() hands back.

A dark code window showing a print call with sep and an input prompt

In short

  • `print()` sends text to STDOUT; separate values with commas or control them with `sep` and `end`.
  • An f-string, `f"{name} scored {marks}"`, is the shortest way to build readable output.
  • `input()` always returns a string, even when the user types digits.
  • Convert before doing maths: `int(input("Age: "))`; a bad value raises `ValueError`.

1

type input() ever returns: str

Python docs

2

print() keywords that change formatting: sep and end

Python docs

STDOUT and STDIN

STDOUT is the standard output stream: the screen, or the terminal window your program runs in. STDIN is the standard input stream: the keyboard. print() writes to the first, input() reads from the second.

The program

# io.py — writing out, reading in

name = "Asha"
marks = 88

print("Name:", name, "Marks:", marks)          # comma separated
print("Name:", name, sep=" | ", end=" !!")
print()
print(f"{name} scored {marks} out of 100")     # f-string
print("%s scored %d" % (name, marks))          # old style, still seen
print("Total is {:.2f}".format(12.3456))       # rounded to 2 places

age = input("Your age: ")
print("Next year you will be", int(age) + 1)

How it works, step by step

  1. With several values, print() puts a space between them by default.
  2. sep=" | " replaces that space; end=" !!" replaces the newline at the end, so the next print continues on the same line.
  3. An f-string is a string with an f in front; anything inside curly brackets is evaluated and inserted.
  4. {:.2f} means "a float with two decimal places", useful for money and marks.
  5. input("Your age: ") shows the prompt and waits. Whatever the user types comes back as a string.
  6. int(age) converts that string to a number, and only then can 1 be added.

What it prints

Name: Asha Marks: 88
Name:  | Asha !!
Asha scored 88 out of 100
Asha scored 88
Total is 12.35
Your age: 42
Next year you will be 43

Which formatting style to use

StyleExampleVerdict
f-stringf"{name}: {marks}"Readable, fast, the modern default
.format()"{}: {}".format(name, marks)Fine, common in older code
% operator"%s: %d" % (name, marks)Oldest; recognise it, prefer f-strings

Common mistakes

  • input() + 1 — text plus number. Convert first.
  • int(input()) with no prompt, leaving the user staring at a blank screen.
  • Forgetting that end replaces the newline, so the following output runs onto the same line.
  • Trusting the input. A user who types "twenty" makes int() raise ValueError; wrap it in try when the program must survive it.

Practice

Try these before moving on. Each one uses only what this lesson covered.

  1. Read a name and a city with `input()` and print a sentence using an f-string.
  2. Read two numbers, convert both, and print their sum, difference and product.
  3. Print three values separated by a dash using `sep="-"`, and print a line without a newline using `end=""`.

Sources

Frequently asked questions

What does input() return in Python?

Always a string, whatever is typed. Even "42" comes back as text, so convert with int() or float() before doing arithmetic.

How do I print without a newline?

Pass end="": print("Loading", end=""). The next print continues on the same line.

What is an f-string?

A string prefixed with f where curly brackets contain expressions: f"{name} scored {marks}". Python replaces each bracket with its value.

Can input() read numbers directly?

No. It has no type parameter. Read the text and convert: age = int(input("Age: ")). Wrap it in try and except if the value might not be a number.

Vinay Garg

Vinay Garg

Assistant Professor of Computer Science at Multani Mal Modi College, Patiala. Twenty-five years of teaching web programming, and a habit of building the things I teach about.