Concepts / String Concatenation and Conversion

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.

  • Programming

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.

callswaits forEnter returns inputProgram executionrunninginput()waiting for typingKeyboard inputuser types and pressesEnterProgram executionresumes
What happens to program control before, during, and after the user types input and presses Enter?

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)

Enter submitsstores returned stringcombines with greeting textAlicetyped textinput()returns a stringname"Alice"Greeting"Hello, Alice"
How does text move from the user's keyboard through input() and into a stored variable?

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.

ValueTypeUse described in the source
"15"StringText returned by input()
15IntegerNumber 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.

python
pass toreturnsadd 1"15"stringint()converts returned text15integer1615 + 1
What changes when a string returned by input() is passed to int(), and what type is stored afterward?

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

EASY

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?

  • The string "Alice"
  • The integer value Alice
  • The prompt string
  • Nothing, because input() only displays text
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

  1. input() displays its prompt, pauses execution, waits for the user to press Enter, and returns the entered value as a string.
  2. Always provide a prompt string so the user knows what to enter.
  3. Use int() to convert string input to an integer before performing arithmetic.
  4. The concise numeric-input pattern is int(input("prompt")).
  5. 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.