{ }codelper

Reference › Python

Python input(): Read User Input

input() reads one line of input and returns it as a string. It takes off the newline at the end of the line and leaves any other spaces as they are. Even if a number is entered, what you get is a string, so you have to convert it to a number before you calculate.

variable = input(prompt)

Reading one line

Each call to input() reads the next line. To read two lines, call it twice.

name = input()city = input()print("Hello,", name)print(len(city), city)
Input
Mina
New York
Output
Hello, Mina
8 New York

Edit and run it →

Even when there is a space inside the line, as in New York, it reads the whole line.

Converting to a number

Wrap it in int() for an integer or float() for a decimal number. Both ignore spaces before and after on their own.

n = int(input())x = float(input())print(n * 2)print(x / 2)print(type(n).__name__, type(x).__name__)
Input
21
3.5
Output
42
1.75
int float

Edit and run it →

When several numbers come on one line, you have to cut the line with split() first. a, b = map(int, input().split()) is that pattern.

Reading several lines

The most common input gives the number of lines, n, on the first line, with n lines below it. Read n first, then loop that many times and read one line each time.

n = int(input())scores = []for _ in range(n):    scores.append(int(input()))print(scores)print(sum(scores))
Input
3
70
85
90
Output
[70, 85, 90]
245

Edit and run it →

When you don't use the loop variable, the convention is to name it _. Shortened to one line, it is scores = [int(input()) for _ in range(n)].

When you don't know how many lines: read to the end

If you are not told the number of lines, you have to read until the input ends. Instead of input(), loop over sys.stdin with for. It hands you one line at a time, and when the input ends, the loop ends too.

import sys count = 0total = 0for line in sys.stdin:    total += int(line)    count += 1print(count, total)
Input
5
10
15
20
Output
4 50

Edit and run it →

Unlike with input(), a line read this way still has the newline at the end. int() doesn't mind, but when you use it as a string, take it off with line.rstrip("\n"). sys.stdin.readline(), which people use because it is fast when there is a lot of input, also returns the line with the newline still attached.

Common mistakes

Calculating without converting to a number

n = input()
print(n * 2)

→ With the input 5, you get 55, not 10

What input() gives is the string "5", so * 2 means repeat it twice. Read it with n = int(input()).

int(input()) when the line has two numbers

n = int(input())

→ ValueError: invalid literal for int() with base 10: '3 4'

int() can convert only a string that holds a single number. Read numbers separated by spaces with a, b = map(int, input().split()). Passing a decimal number (3.5) to int() gives the same error.

Reading more than the input has

a = input()
b = input()

→ EOFError: EOF when reading a line

This error comes when you call input() again with no lines left to read. Count the lines again in the problem's input format. It often happens when you try to read values that all come on one line with several input() calls.

Adding a prompt in a judged problem

n = int(input("n: "))
print(n * 2)

→ The output is n: 10, not 10

The prompt in input("n: ") goes to the output too. A judge that compares your output with the answer character by character marks it wrong, so when you solve problems, write input() with empty parentheses.

Practice problems

See also