Skip to content
FollowtheDamnCode

Reading Input

input(), and turning what the user typed into numbers.

4 minute read

Big idea

input() waits for the user to type a line and gives it back as a string.

The idea

name = input()

The program stops, waits for a line of typing, and when Enter is pressed, name refers to whatever was typed (without the Enter).

In this course, what the "user" types comes from the input box under the code. Change it and run again:

Say hello to anyone
name = input()
print("Hello,", name + "!")
input
Ada
output
Hello, Ada!

Run and edit it in the app.

A prompt

input can show a message first. It prints the message without a new line, so the typing appears right after it:

With a prompt
name = input("What's your name? ")
print("Nice to meet you,", name)
input
Grace
output
What's your name? Nice to meet you, Grace

Run and edit it in the app.

Here the output looks squashed because the input box isn't shown inline — in a real terminal, "Grace" would appear after the prompt as you typed it.

input() always gives a string

Even if the user types 7, you get the text "7". To do maths, convert it:

Fix the bug: add the two numbers
a = input()
b = input()
print(a + b)
input
3
4
output
7

Run and edit it in the app.

The fix: a = int(input()) — read the line, then convert it.

Watch it

Several values on one line

split() cuts a line into pieces at the spaces. Combined with map, you can read several numbers at once:

Two numbers on one line
a, b = map(int, input().split())
print(a * b)
input
6 7
output
42

Run and edit it in the app.

Read it inside out: input() gets "6 7", .split() makes ["6", "7"], map(int, ...) converts each one, and a, b = ... unpacks them. You'll meet each of these properly later — for now, it's a recipe you can copy.

The user types 25. What is type(input())?

Think of it like

A waiter taking your order. The program asks, waits patiently, then writes down exactly what you said — as words, even if you said a number.

Key terms

  • input() — Reads one line typed by the user, as a string.
  • Prompt — The message shown before waiting for input.
  • split() — Cuts a string into a list of pieces at the spaces.

Check yourself

What type does input() return?

How do you read a whole number from the user?

What does "3 4".split() give?