Concepts / Data Types: Strings, Integers, and Floats

Data Types: Strings, Integers, and Floats

input() pauses your program and waits for the user to type something on their keyboard, then returns what they typed as a string.

  • Programming

From Script to Conversation

A hard-coded program always works with the values written into its script. An interactive program can ask the user for information, process the response, and then produce an answer. Python's input() function provides the pause between those stages: it waits for keyboard input, then gives the program what the user typed.

The Pause Before Enter

calls input()guidesresponse is enteredwaits forreleasesexecution resumesProgramrunningPromptdisplayedKeyboard inputuser types a responseinput()waitingEnterpressedString valuereturned to programProgramcontinues
What happens to program control before, during, and after the user types a response and presses Enter?

When Python reaches input(), it pauses the program and waits for the user to type on the keyboard. A prompt string can be displayed before the pause so the user knows what to enter. The program resumes after the user presses Enter. The value returned at that point is the text the user typed, represented as a string.

python
Output
What is your name? Alice
Hello, Alice

In this example, Python displays the prompt first. It then waits while the user types Alice and presses Enter. The returned string is stored in name, and the next print() statement uses that stored value.

Strings Before Numbers

A value returned by input() remains a string even when it looks like a number. For example, typing 15 gives input() the text "15". Python does not automatically make that text an integer merely because the characters are digits. To use the response in arithmetic, pass the returned string to int().

input() returnspassed toproducescan be used in15typed by user"15"stringint()converts text15integer15 + 1arithmetic
How does the value returned by input() change when it passes through int(), and what data type does each step contain?

Converting before calculating

A user enters 15. Store the response, convert it to an integer, and add 1.

Capture: input() waits for the user and returns the typed response as the string "15".

Convert: int() changes the returned string into the integer 15.

Calculate: The integer can now be used in the arithmetic expression 15 + 1.

Display: The result can be converted to a string with str() when it must be joined with surrounding text in a print() statement.

The numeric result is 16.

python
Output
How old are you? 15
Next year you will be 16

The conversion can also happen in one line. In int(input("How old are you? ")), input() first waits and returns a string. The surrounding int() then converts that returned string before the value is assigned to age. This nested form is the standard concise way to obtain integer input.

Choosing the Right Conversion

  • Assuming digits entered through input() are already integers

    input() returns the response as a string, even when the response consists of digits. The value has not been converted for integer arithmetic.

    Fix: Use value = int(input("Enter a number: ")) before adding 1.

  • Omitting the prompt string

    The program waits for keyboard input without telling the user what to enter.

    Fix: Pass a guiding string such as input("Enter a number: ").

  • Converting too late

    The response is still a string until int(value) is applied. Any arithmetic must use the converted integer.

    Fix: Convert as part of the assignment when the value is intended for arithmetic: value = int(input("Enter a number: ")).

  • Treating the displayed form as proof of the data type

    The source rule concerns what input() returns: the returned value is the string "15".

    Fix: Track the conversion explicitly: input() returns a string, and int() produces an integer.

StageExpressionValue formUse in this lesson
User response receivedinput("Enter a number: ")StringCaptures what the user typed
Response convertedint(input("Enter a number: "))IntegerAllows the captured value to be used in arithmetic
Floating-point conversionNot covered in the source materialNot specifiedThis lesson does not establish float behavior

Try the Input Flow

EASY

Write a short Python program that asks the user to enter a number, converts the response to an integer, adds 10, and displays the result.

Hints
  • Put a prompt string inside input().
  • Wrap the input() call in int() before assigning the value.
  • Use a separate expression to add 10.

What do you think happens?

Before running the program, predict what type of value is assigned to response after the user types 8 and presses Enter.

  • The string "8"
  • The integer 8
  • The float 8.0
Reveal answer

Answer: The string "8"

input() always returns a string. The response becomes an integer only when int() is applied.

Key Takeaways

  1. input() pauses execution, displays its prompt, waits for keyboard input, and resumes after the user presses Enter.
  2. The value returned by input() is always a string, including when the user types digits.
  3. Use int() to convert string input into an integer for arithmetic.
  4. The concise pattern int(input("prompt")) captures and converts numeric input in one line.
  5. This lesson establishes strings returned by input() and integers produced by int(); it does not develop float behavior.

Key Takeaways

  • input() creates an interaction by pausing the program until the user types a response and presses Enter.
  • A prompt string tells the user what to enter before the program waits.
  • input() returns typed data as a string, so numeric-looking input is still text at first.
  • int(input("prompt")) converts the response to an integer immediately, allowing arithmetic.
  • The provided material focuses on strings and integers and does not specify how floats behave.