Type Conversion: int(), float(), and str()
The important question is not only what the user types. You must also identify what kind of value the program receives and whether that value needs conversion before later operations.
Try it: Type conversion: int(), float(), str(), bool()
Exactly how int(), int(text, base), float(), str() and bool() turn a value into another type: Python's real parsing rules for text (whitespace, sign, underscores, 0x/0o/0b prefixes, inf/nan, exponents) and its truthiness rules.
How it works
- int(text) strips surrounding whitespace, allows one sign, then reads digits (underscores only between digits); any other character raises ValueError: invalid literal for int().
- int(text, base) reads digits in that base; base 0 takes the base from a 0x / 0o / 0b prefix like a Python literal.
- float(text) accepts decimals like '.5', '5.', '1e3' and the names inf, infinity and nan, then rounds to the nearest double.
- int() of a float truncates toward zero; str() gives the printed form (repr digits for floats).
- bool() is truthiness: '', 0, 0.0 and None are False; every non-empty string — even '0' or 'False' — is True.
Default run (8 steps): int(' -0x_1F ', 0): the argument is a str of 10 characters. … int(' -0x_1F ', 0) → -31
Simplified: The argument is a Python literal typed as text (ASCII only; escapes \t \n \r). Unicode digits and bytes arguments are not modelled.
Loading the simulation…
From Fixed Values to User Data
A program becomes interactive when it can ask the person running it for information. A banking app may ask for an account number, a game may ask for a name, and a calculator may wait for numbers. Python provides this interaction through input(). The function pauses program execution and waits for keyboard input from the user.
The important question is not only what the user types. You must also identify what kind of value the program receives and whether that value needs conversion before later operations.
The Pause at input()
When execution reaches input(), the program stops at that point. The cursor waits for the user. The next part of the program does not run until the user types a response and presses Enter. After that response is captured, execution continues with the following statement.
What do you think happens?
A program reaches input(), and the user has not typed anything yet. What happens to the statement after input()?
Reveal answer
It waits until the user provides input.
input() pauses program execution. After the user types and presses Enter, input() returns the entry and execution moves forward.
What input() Returns
The value returned by input() is always a string, regardless of what the user types. If the user types a name, the result is text. If the user types 25, the result is still the string '25', not the integer 25. If the user types a decimal-looking value, the result also begins as text.
The Input Pipeline
Assigning the result of input() to a variable creates a useful pipeline. First, input() reads from the keyboard. Next, it returns the user's entry as a string. The assignment stores that string in a variable. The program can then use the stored value in later output or operations.
Always assign the result of input() to a variable when the program needs to use the entered data later. A stored value can be referred to in later parts of the program.
Choosing a Conversion
Conversion means changing the form in which a value is represented so that the program can use it appropriately. For input data, int() and float() are the conversions named for numeric use. Convert string input to int() or float() before performing arithmetic operations. Use int() when the intended numeric form is an integer, and float() when the intended numeric form is a floating-point value. str() is the conversion named for string form; input() already returns a string, so no numeric conversion is needed when the program simply needs the entered text.
| Function | Role in this topic | When it matters |
|---|---|---|
| int() | Converts string input for integer use | Before arithmetic that needs an integer |
| float() | Converts string input for floating-point use | Before arithmetic that needs a decimal-capable numeric form |
| str() | Represents a value as a string | When the program needs text; input() already supplies a string |
The three conversion functions named in this lesson
A Complete Trace
Collecting a Name
Trace what happens when a program asks for a name, the user types Alice, and the program later displays the stored name.
- 1Reach the call
Execution reaches
input(), so the program pauses and waits for keyboard input. - 2Receive the entry
The user types Alice and presses Enter.
input()captures the entry as the string 'Alice'. - 3Assign the result
The result is assigned to the variable name, so name stores the string 'Alice'.
- 4Continue execution
Execution moves to the next statement, which can use the value stored in name.
The later output uses the string 'Alice'. Nothing after input() runs until the user provides the entry.
Preparing Numeric Input
Trace what must happen when a user types 25 and the program needs to use that entry in arithmetic.
- 1Pause
The program reaches
input()and waits for the user to type and submit an entry. - 2Receive text
The entry 25 is returned as the string '25', even though it looks like a number.
- 3Convert
The program converts the string with
int()orfloat(), depending on the numeric form required by the later arithmetic. - 4Use
The converted value is then ready for the intended arithmetic operation.
The essential sequence is input(), string result, numeric conversion, and then arithmetic.
Mistakes with Conversion
Common mistake Expecting input() to return a number automatically
What it looks likeThe user types 25, and the program treats the returned value as the integer 25 without conversion.
Why it failsinput()returns the user's entry as a string regardless of what was typed.Do this insteadConvert the string with int() or float() before performing arithmetic.
Common mistake Forgetting to store input that is needed later
What it looks likeThe program calls input() but does not assign the result to a variable.
Why it failsThe program has no named stored value to use in later parts of the program.
Do this insteadAssign the result of input() to a variable whenever the data will be used later.
Common mistake Expecting later statements to run while input() is waiting
What it looks likeA learner expects output from a later statement before the user has entered a response.
Why it failsinput()pauses execution until the user types and presses Enter.Do this insteadTrace the statements in order: reach input(), wait, receive the entry, assign it if needed, and then continue.
Common mistake Converting every entry without considering its use
What it looks likeA name is treated as numeric input even though the program only needs the text.
Why it failsThe conversion should match what the program needs to do with the data.
Do this insteadKeep text as a string when the program needs the entered words; use int() or float() for arithmetic.
Practice the Trace
Trace this interaction in words: a program calls input(), the user types 25, and the program later needs the entry for arithmetic. Identify when execution pauses, what input() returns, where the returned value is stored, and whether int() or float() is the better conversion for the intended numeric form.
Hints
- Begin at the
input()call. - State explicitly that the returned value is the string '25'.
- Choose
int()for an integer form orfloat()for a floating-point form.
A complete answer identifies four stages: execution pauses at input(), the user supplies an entry, input() returns that entry as a string and the assignment stores it, and the program converts the string with int() or float() before arithmetic.
Test interactive programs with different entries. Verify that execution really waits, that the stored value matches the text entered, and that numeric input is converted before arithmetic is attempted.
Key Takeaways
- 1
input()pauses program execution and waits for keyboard input. - 2
input()always returns the user's entry as a string, even when the entry looks like a number. - 3Assign the result of
input()to a variable when the program needs to use the entry later. - 4Convert string input with
int()orfloat()before performing arithmetic. - 5Use the string form when the program needs text, and trace the call in order: pause, receive, assign, convert if needed, and continue.
At a glance
input()pauses execution until the user types and presses Enter.- The result of
input()is always a string. - A variable stores the returned string so later statements can use it.
int()andfloat()prepare string input for arithmetic, whilestr()represents data in string form.- Correctly tracing input means following the sequence of waiting, receiving, assigning, converting when needed, and continuing.