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.
- 1Capture
input()waits for the user and returns the typed response as the string "15". - 2Convert
int()changes the returned string into the integer 15. - 3Calculate
The integer can now be used in the arithmetic expression 15 + 1.
- 4Display
The result can be converted to a string with
str()when it must be joined with surrounding text in aprint()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
Common mistake Assuming digits entered through input() are already integers
What it looks likevalue = input("Enter a number: ") result = value + 1Why it failsinput()returns the response as a string, even when the response consists of digits. The value has not been converted for integer arithmetic.Do this insteadUse value = int(input("Enter a number: ")) before adding 1.
Common mistake Omitting the prompt string
What it looks likeresponse = input()Why it failsThe program waits for keyboard input without telling the user what to enter.
Do this insteadPass a guiding string such as input("Enter a number: ").
Common mistake Converting too late
What it looks likevalue = input("Enter a number: ") text = "You entered " + value number = int(value)Why it failsThe response is still a string until
int(value)is applied. Any arithmetic must use the converted integer.Do this insteadConvert as part of the assignment when the value is intended for arithmetic: value = int(input("Enter a number: ")).
Common mistake Treating the displayed form as proof of the data type
What it looks likeThe user types 15, so the program is assumed to have received the integer 15.
Why it failsThe source rule concerns what
input()returns: the returned value is the string "15".Do this insteadTrack 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 |
The source material establishes the string result of input() and the integer conversion performed by int(). It does not provide a definition or conversion procedure for floats.
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 inint()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
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. - 2The value returned by
input()is always a string, including when the user types digits. - 3Use
int()to convert string input into an integer for arithmetic. - 4The concise pattern
int(input("prompt"))captures and converts numeric input in one line. - 5This lesson establishes strings returned by
input()and integers produced byint(); it does not develop float behavior.
At a glance
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.