1.2.14 · D1Introduction to Programming (Python)

Foundations — Input with `input()` — always returns string, conversion needed

1,659 words8 min readBack to topic

This page assumes you know nothing. We will build every word, function, and symbol the parent note uses, in an order where each piece stands on the one before it. Look at the pictures — they carry the meaning.


1. A character — the smallest piece

Look at the figure. The keyboard key 5 produces the character '5'. That character is a tiny picture of a five — not the quantity five.

Figure — Input with `input()` — always returns string, conversion needed

Why the topic needs this: if you don't see that '5' (drawing) and 5 (quantity) are different things, nothing else about input() will make sense.


2. A string — characters in a row

Figure — Input with `input()` — always returns string, conversion needed

In the picture, "42" is a row of two drawings: the character '4' then the character '2'. Together they still form a picture, not the quantity forty-two.

  • The quotes "..." are the fence that says "everything inside is text."
  • "42" and 42 look almost the same on paper but are different kinds of thing.

Why the topic needs this: input() always gives you a str — so you must know exactly what a string is before you can convert it.

See Variables and Data Types for the full family of types.


3. An integer and a float — real quantities

Figure — Input with `input()` — always returns string, conversion needed

The picture shows a number line. An int lands exactly on a marked tick (3). A float can land between ticks (3.14). Both are quantities you can do arithmetic with — unlike a string, which is just ink.

Why the topic needs this: these are the targets of conversion. You read a string, then aim it at int (whole) or float (decimal) depending on the data's meaning.

More at Variables and Data Types.


4. A variable and the = sign

Why the topic needs this: the core pattern name = input(...) uses = to catch what the microphone recorded. Without understanding the box, x = input() is mysterious.


5. A function and its call name(...)

Figure — Input with `input()` — always returns string, conversion needed

The picture shows a machine box. int("42") feeds the string "42" into the int machine; out comes the quantity 42. The round brackets (...) are the mouth of the machine.

Functions the parent note uses:

  • input(prompt) — the microphone machine: waits, records, returns a str.
  • int(s) — the reader machine: turns a digit-string into a whole quantity.
  • float(s) — turns a decimal-string into a decimal quantity.
  • print(...) — the speaker machine: shows a value on screen. See print() Function.
  • type(...) — the label reader: tells you what kind of thing a value is.

Why the topic needs this: every line in the parent note is a function call. int(input("Age: ")) is one machine feeding another — the microphone's output goes straight into the reader.

Conversion machines are covered deeply in Type Conversion (Casting).


6. Nesting — a machine inside a machine

Why the topic needs this: the recommended one-line pattern value = int(input(prompt)) is exactly this nesting. You must see the order: microphone first, reader second, then store in the box.


7. The + and * symbols — meaning depends on type

Expression Types Result Job done
3 + 4 int + int 7 add
"3" + "4" str + str "34" join
"ab" * 3 str * int "ababab" repeat
"ab" * "3" str * str error undefined

Why the topic needs this: this is the exact trap the parent note warns about. "3" + "4" = "34" because both are strings. Understanding overloading explains why forgetting to convert gives 34 instead of 7.

Explored further in Operators in Python and String Concatenation vs Arithmetic.


8. An error / exception

Why the topic needs this: conversion can fail. Knowing that a bad string raises ValueError tells you why real programs guard input with Exceptions and try-except.


Prerequisite map

character - one typed symbol

string - row of characters

input() returns a string

int and float - real quantities

conversion int() float()

function call name(...)

variable and = assignment

value = int(input(prompt))

+ and * depend on type

why conversion is needed

exceptions ValueError TypeError

Input & Conversion topic

Everything above feeds into the parent topic: the main note.


Equipment checklist

Test yourself — cover the right side and answer:

What is a character?
A single typed symbol (letter, digit-symbol, space), a drawing not a quantity.
What is a string (str)?
A row of characters between quotes, e.g. "42"; it is text, not a number.
How does '5' (the character) differ from 5 (the quantity)?
'5' is a drawing of five; 5 is the actual amount you can do math with.
What is an int vs a float?
int is a whole-number quantity (42); float is a decimal-number quantity (3.14).
What does the = sign do in Python?
Assignment — puts the value on the right into the box (variable) named on the left; read it as .
What does name(...) do?
Calls (runs) a function, feeding whatever is in the brackets into it and getting a result back.
In int(input("x")), which runs first?
The inner input("x") runs first; its string result is then fed to int.
What does + do for two strings vs two numbers?
For strings it joins ("3"+"4"="34"); for numbers it adds (3+4=7).
What does "ab" * 3 produce, and why does "ab" * "3" fail?
"ababab" (repeat); the second fails because you can't multiply a string by a string (TypeError).
What is a ValueError?
An exception raised when a function like int() gets a string it can't convert, e.g. int("twenty").