User Input and Type Conversion¶
By the end of this lesson you'll be able to:
- Use
input()to ask the user a question and read their answer - Convert between data types using
int(),float(),str(), andbool() - Check a value's type with the
type()function - Validate user input so your program doesn't crash on bad data
Until now your programs have run the same way every time.
With input(), your program can have a conversation with the user — asking questions and responding to their answers.
Welcome to Chapter 13!
Today your programs come alive! Instead of running the same way every time,
they'll ask the user questions and respond to the answers.
Get ready to build your first truly interactive programs! Let's code it together!
The input() Function¶
input() stops the program, prints a message (called a prompt), and waits for the user to type something and press Enter.
Whatever the user types comes back to your program as a string.
The key word to remember: input() always returns a string, even if the user types a number.
1 2 | |
Scratch Bridge
In Scratch, the "Ask ___ and wait" block pauses the program and stores the answer in a special variable called answer.
In Python, input("your question") does exactly the same thing — it pauses and returns the answer as a string.
Type Conversion¶
Since input() always returns a string, you need to convert it before doing math.
Before we dive in, let's define three terms:
- Integer (int): a whole number with no decimal point — 5, -12, 0
- Float: a number with a decimal point — 3.14, -0.5, 100.0
- Type conversion: changing a value from one data type to another
int() — Convert to Integer¶
int() converts a string (or float) to a whole number.
If the string contains letters, int() raises a ValueError — so always convert only after checking the input looks like a number.
1 2 3 | |
float() — Convert to Decimal Number¶
float() converts a string to a number with a decimal point.
Use it when you need precision beyond whole numbers — for prices, measurements, or temperatures.
1 2 3 | |
str() — Convert to String¶
str() converts a number (or other value) into a string.
You need this when you want to join a number with text using +.
1 2 | |
bool() — Convert to True or False¶
bool() converts a value to True or False.
The rules for what becomes False are worth memorizing:
| Value | bool() result |
|---|---|
0 |
False |
"" (empty string) |
False |
None |
False |
| Anything else | True |
1 2 3 4 | |
The type() Function¶
type() tells you what data type a value is.
It's incredibly useful for debugging — when you're not sure whether you're working with a string or a number, just ask Python.
If you type a number like 42, you'll see <class 'str'> first, then <class 'int'> after the conversion.
String Methods: lower(), upper(), strip()¶
Before you convert or validate input, it's a good idea to clean it first.
s.lower()— converts all letters to lowercase ("Hello"→"hello")s.upper()— converts all letters to uppercase ("hello"→"HELLO")s.strip()— removes leading and trailing spaces (" hi "→"hi")
These three methods are very useful for input handling:
1 2 3 4 5 6 | |
Input Validation Basics¶
Validation means checking that the user's input is safe to use before using it.
A simple approach is to ask str.isdigit() — a string method that returns True if all characters are digits.
Before calling int(), you can use isdigit() to avoid a crash:
1 2 3 4 5 6 7 8 | |
Try it twice: once with a number like 7, and once with letters like hello.
print() Tips¶
print() is more powerful than it looks. A few useful tricks:
| Feature | Example | Output |
|---|---|---|
| Multiple values | print("a", "b", "c") |
a b c |
| Custom separator | print("a", "b", sep="-") |
a-b |
| No newline at end | print("Hello", end=" ") |
stays on same line |
| Empty line | print() |
blank line |
Learning Check¶
Spot the Bug!
The program below is supposed to add two numbers entered by the user.
But it has a bug — it concatenates strings instead of adding numbers!
Fix it so the math actually works.
If you enter 3 and 4, the buggy version prints 34 instead of 7.
Fix it by converting a and b to int() before adding them.
Experiments¶
Try these changes. Predict what will happen first, then run it to check!
-
Change the greeting lab to ask for the user's favorite color. Print
"My favorite color is [color] too!". You'll know it worked when the program echoes back whatever color you typed. -
Ask the user for a temperature in Celsius. Convert it:
fahrenheit = (celsius * 9/5) + 32. Print the result. You'll know it worked when entering100prints212.0. -
Ask the user for their name, then use
.upper()to print it in capitals. You'll know it worked when typingmontyprintsMONTY. -
In the validation demo, try typing a negative number like
-5. Doesisdigit()accept it? You'll know it worked when you see that-5fails theisdigit()check — and you understand why! -
Use
type()to print the types of3.14,True, andNone. You'll know it worked when you seefloat,bool, andNoneType.
Amazing Progress!
Your programs can now have real conversations with users!
You've learned input(), type conversion, validation, and string cleaning.
These skills appear in nearly every real Python program. Let's keep coding together!