Input and type conversion

Introduction

input() lets a program ask the user for something. Whatever the user types comes back as text, even when they type a number, so most programs convert it first.

A program that only uses fixed values does the same thing every time. Reading input is what makes a program useful to more than one person, and most of the mistakes beginners make here come from one fact: everything typed arrives as text.

How it works

Reading text

name = input("Your name: ")
print("Hello,", name)

Input used: Rahim

Open in compiler

input("Your name: ") prints the prompt, waits for one line, and gives back what was typed. In the visualizer, type your answers in the Input box, one line per input() call. This example was recorded with the input Rahim.

Text is not a number

a = input()
b = input()
print(a + b)

Input used: 5 ⏎ 7

Open in compiler

The user typed 5 and 7, but the output is 57, not 12. Both values are strings, and + on strings joins them. Check the variables panel: a is '5', with quotes, which marks it as text.

Neat output with f-strings

cgpa = float(input())
print(f"Your CGPA is {cgpa:.2f}")

Input used: 3.756

Open in compiler

An f-string starts with f before the quote. Anything in curly brackets is replaced by its value, and :.2f shows exactly two decimal places, so 3.756 prints as 3.76.

Syntax

name = input("Your name: ")
age = int(input())
height = float(input())
print(f"{name} is {age}")
print(f"{height:.2f}")

Example

a = int(input())
b = int(input())
print(a + b)

Input used: 5 ⏎ 7

Open in compiler

int(...) turns text into a whole number, and float(...) turns it into a decimal number. Now a is 5 without quotes, and a + b is 12. To go the other way, str(12) gives the text '12'.

Line by line

  1. a = int(input()) reads a line, turns it into a whole number and stores it.
  2. b = int(input()) does the same for the second line.
  3. print(a + b) adds two numbers, so the result is 12, not 57.

Expected result

With this input:

5
7

it prints:

12

In the real world

Every form on the web delivers text, even a box marked "age". Servers have to convert and check that text before they can use it, and a surprising number of software bugs start at exactly this point.

Common mistakes

  • Forgetting int(): input() + input() joins text, as glue shows.
  • int("3.5") is a ValueError. Use float() for numbers with a decimal point.
  • Typing two numbers on one line when the program calls input() twice. Each input() reads one whole line.

Best practices

  • Convert input on the same line that reads it.
  • Use float only when a value can really have decimals.
  • Use f-strings rather than joining many pieces with +.

Summary

input reads one line as text. int and float convert it to numbers, and str converts the other way. An f-string puts values inside text, and :.2f shows two decimal places.

Practice

Ask for a product's price and the quantity, then print the total cost with an f-string showing two decimal places.

Quick check

1. The user types 10. What type is the value input() returns?
2. What does print('4' + '4') show?
3. Which line reads a decimal number such as 2.5?

Key takeaways

  • input always gives text.
  • Convert with int or float before doing arithmetic.
  • f-strings make output easy to read.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.