String Concatenation and Conversion
input() pauses your program and waits for the user to type something on their keyboard, then returns what they typed as a string.
From Fixed Data to Interaction
A program becomes interactive when it can ask the user for information, use what the user entered, and then respond. The input() function provides this interaction: it pauses the program, waits for keyboard input, and returns what the user typed as a string.
Capturing Text with a Prompt
Pass a prompt string to input() so the user knows what to enter. The prompt is displayed before the program pauses. After the user types a response and presses Enter, input() returns the typed text, and that returned string can be stored in a variable.
name = input("What is your name? ") print("Hello, " + name)
The String Returned by input()
input() always returns a string, even when the user types digits. For example, entering 15 produces the string "15", not the integer 15. A string can be combined with other text, but it must be converted before it is used for arithmetic.
| Value | Type | Use described in the source |
|---|---|---|
| "15" | String | Text returned by input() |
| 15 | Integer | Number available for arithmetic after int() conversion |
Converting Text into an Integer
Use int() when numeric input must be used for arithmetic. The call int(input("prompt")) first obtains the user's input as a string and then converts that returned string to an integer. The converted integer can then be used in a calculation.
Adding One to User Input
A user enters 15. Convert the entered value to an integer and add 1.
Receive input: input() returns the string "15" after the user types 15 and presses Enter.
Convert input: int() converts the string "15" to the integer 15.
Calculate: The integer 15 can be used in arithmetic, so adding 1 produces 16.
Prepare text output: The numeric result is converted back to a string with str() when it must be concatenated with text in the print() statement.
The calculation produces 16, and the displayed message can include that result as text.
When you know the input should be numeric, combine the conversion and input operation as int(input("prompt")). This converts the value immediately as it is assigned to the variable.
Mistakes with Input Types
Calling input() without explaining what the user should enter.
The program pauses, but the user is not given a prompt describing the expected input.
Fix:
Provide a prompt string, such as input("Enter a number: ").Treating numeric-looking input as an integer automatically.
input() returns the entered value as a string, even when the user types numbers.
Fix:
Use int(input("Enter a number: ")) when the value is needed for arithmetic.Forgetting to convert a calculated result before combining it with text.
The source example converts the result with str() so it can be concatenated with the text in the print() statement.
Fix:
Use print("The result is " + str(result)).
Try the Interaction
Plan a short interactive program that asks the user for a number, converts the response to an integer immediately, adds 1, and prepares a text response containing the result.
Hints
- Give input() a prompt string.
- Nest input() inside int() so the stored value is an integer.
- Use str() when combining the calculated result with text in the displayed response.
What do you think happens?
A program calls input("What is your name? ") and the user types Alice followed by Enter. What does the variable assigned to input() contain?
Reveal answer
Answer: The string "Alice"
The prompt is displayed first, input() waits for the response, and the entered text is returned as a string and stored in the variable.
Key Takeaways
- input() displays its prompt, pauses execution, waits for the user to press Enter, and returns the entered value as a string.
- Always provide a prompt string so the user knows what to enter.
- Use int() to convert string input to an integer before performing arithmetic.
- The concise numeric-input pattern is int(input("prompt")).
- Use str() when a calculated numeric result must be concatenated with text for display.
Key Takeaways
- input() pauses a program and returns the user's keyboard input as a string.
- A prompt string guides the user before the program waits.
- Numeric-looking input remains a string until int() converts it to an integer.
- The pattern int(input("prompt")) performs the conversion immediately.
- Use str() when combining a calculated result with surrounding text.