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)- 1
input()displays the prompt and pauses the program. - 2The user types Alice and presses Enter.
- 3
input()returns the string "Alice", which is stored in name. - 4
print()displays a greeting formed by combining text with the stored name.
- The prompt tells the user what to enter.
- The entered text is stored in name.
- The
print()statement combines the greeting text with name.
What is your name? Alice
Hello, AliceThe 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 |
The characters look similar, but the string and integer represent different kinds of values.
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.
- 1Receive input
input()returns the string "15" after the user types 15 and presses Enter. - 2Convert input
int()converts the string "15" to the integer 15. - 3Calculate
The integer 15 can be used in arithmetic, so adding 1 produces 16.
- 4Prepare text output
The numeric result is converted back to a string with
str()when it must be concatenated with text in theprint()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
Common mistake Calling input() without explaining what the user should enter.
What it looks likevalue = input()Why it failsThe program pauses, but the user is not given a prompt describing the expected input.
Do this insteadProvide a prompt string, such as input("Enter a number: ").
Common mistake Treating numeric-looking input as an integer automatically.
What it looks likevalue = input("Enter a number: ")Why it failsinput()returns the entered value as a string, even when the user types numbers.Do this insteadUse int(input("Enter a number: ")) when the value is needed for arithmetic.
Common mistake Forgetting to convert a calculated result before combining it with text.
What it looks likeprint("The result is " + result)Why it failsThe source example converts the result with
str()so it can be concatenated with the text in theprint()statement.Do this insteadUse 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()insideint()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
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
- 1
input()displays its prompt, pauses execution, waits for the user to press Enter, and returns the entered value as a string. - 2Always provide a prompt string so the user knows what to enter.
- 3Use
int()to convert string input to an integer before performing arithmetic. - 4The concise numeric-input pattern is
int(input("prompt")). - 5Use
str()when a calculated numeric result must be concatenated with text for display.
At a glance
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.