Concepts / Type Conversion: int(), float(), and str()

Type Conversion: int(), float(), and str()

input() pauses program execution and waits for keyboard input from the user.

  • Programming

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.

pausesuser respondsinput returnedinput()program reaches callWaitingkeyboard inputUser entrytype and press EnterNext statementexecution resumes
What happens to the program's execution flow before, during, and after an input() call?

What do you think happens?

A program reaches input(), and the user has not typed anything yet. What happens to the statement after input()?

  • It runs immediately
  • It waits until the user provides input
  • It is skipped permanently
Reveal answer

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.

input()input()input()25user typesstring '25'input() returns3.5user typesstring '3.5'input() returnsAliceuser typesstring 'Alice'input() returns
What data type does input() produce when the user types a number, decimal, or word?

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.

typed datastring resultstored valueKeyboarduser entryinput()returns a stringVariablestores the stringLater useoutput or operation
How does user-entered data move from input() into a variable and then into later program activity?

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.

FunctionRole in this topicWhen it matters
int()Converts string input for integer useBefore arithmetic that needs an integer
float()Converts string input for floating-point useBefore arithmetic that needs a decimal-capable numeric form
str()Represents a value as a stringWhen 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.

Reach the call: Execution reaches input(), so the program pauses and waits for keyboard input.

Receive the entry: The user types Alice and presses Enter. input() captures the entry as the string 'Alice'.

Assign the result: The result is assigned to the variable name, so name stores the string 'Alice'.

Continue 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.

Pause: The program reaches input() and waits for the user to type and submit an entry.

Receive text: The entry 25 is returned as the string '25', even though it looks like a number.

Convert: The program converts the string with int() or float(), depending on the numeric form required by the later arithmetic.

Use: 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

  • Expecting input() to return a number automatically

    input() returns the user's entry as a string regardless of what was typed.

    Fix: Convert the string with int() or float() before performing arithmetic.

  • Forgetting to store input that is needed later

    The program has no named stored value to use in later parts of the program.

    Fix: Assign the result of input() to a variable whenever the data will be used later.

  • Expecting later statements to run while input() is waiting

    input() pauses execution until the user types and presses Enter.

    Fix: Trace the statements in order: reach input(), wait, receive the entry, assign it if needed, and then continue.

  • Converting every entry without considering its use

    The conversion should match what the program needs to do with the data.

    Fix: Keep text as a string when the program needs the entered words; use int() or float() for arithmetic.

Practice the Trace

EASY

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 or float() 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.
  3. Assign the result of input() to a variable when the program needs to use the entry later.
  4. Convert string input with int() or float() before performing arithmetic.
  5. Use the string form when the program needs text, and trace the call in order: pause, receive, assign, convert if needed, and continue.

Key Takeaways

  • 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() and float() prepare string input for arithmetic, while str() represents data in string form.
  • Correctly tracing input means following the sequence of waiting, receiving, assigning, converting when needed, and continuing.