Talking with Your Program
Input Is Always Text
input() always gives your program a
The playground contains:
answer = input("Type any number: ")
print(answer)
print(type(answer))
Press Run, enter 42, and press Enter. The final line will report:
<class 'str'>
The person typed digits, but Python stored the answer as text.
Why input starts as text
A keyboard sends characters. Python cannot know what those characters mean to your program.
Consider the answer 2026. It might represent:
a year that you want to calculate with;
part of an account name;
a door code that should stay as text;
a number in a sentence.
Python leaves that decision to the programmer. input() collects the characters as a string. Your code can convert the string when it needs a numerical
Text that looks like a number
These values are different:
"42"
42
The first is a string. The second is an integer. They may look similar in output, but they support different operations.
If you try to add the integer 1 directly to text returned by input(), Python reports a
age = input("How old are you? ")
next_age = age + 1
Python will not guess whether you meant text or arithmetic. That strictness prevents many silent mistakes.
A person types 12 in response to input(). What type does Python return?
Run the starter program with a few answers. Try 42, 3.5, and a word. type(answer) reports str each time.
Next, you will look more closely at how
Task
Run the program several times. Enter a whole number, a decimal number, and a word. Notice that input() returns a