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.
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
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.
What is your name? Alice
Hello, AliceIn 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().
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.
How old are you? 15
Next year you will be 16The 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.
| Stage | Expression | Value form | Use in this lesson |
|---|---|---|---|
| User response received | input("Enter a number: ") | String | Captures what the user typed |
| Response converted | int(input("Enter a number: ")) | Integer | Allows the captured value to be used in arithmetic |
| Floating-point conversion | Not covered in the source material | Not specified | This lesson does not establish float behavior |
Try the Input Flow
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.
Reveal answer
Answer: The string "8"
input() always returns a string. The response becomes an integer only when int() is applied.
Key Takeaways
- input() pauses execution, displays its prompt, waits for keyboard input, and resumes after the user presses Enter.
- The value returned by input() is always a string, including when the user types digits.
- Use int() to convert string input into an integer for arithmetic.
- The concise pattern int(input("prompt")) captures and converts numeric input in one line.
- 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.