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.

Program executionrunninginput()waiting for typingKeyboard inputuser types and pressesEnterProgram executionresumescallswaits forEnter returns input
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.

Code walkthrough
name = input("What is your name? ")
print("Hello, " + name)
  1. 1
    input() displays the prompt and pauses the program.
  2. 2
    The user types Alice and presses Enter.
  3. 3
    input() returns the string "Alice", which is stored in name.
  4. 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.
Output
What is your name? Alice
Hello, Alice
Alicetyped textinput()returns a stringname"Alice"Greeting"Hello, Alice"Enter submitsstores returned stringcombines with greeting text
How does text move from the user's keyboard through input() and into a stored variable?

The String Returned by input()

Definition

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

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.

Worked example

Adding One to User Input

A user enters 15. Convert the entered value to an integer and add 1.

  1. 1
    Receive input

    input() returns the string "15" after the user types 15 and presses Enter.

  2. 2
    Convert input

    int() converts the string "15" to the integer 15.

  3. 3
    Calculate

    The integer 15 can be used in arithmetic, so adding 1 produces 16.

  4. 4
    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.

Result

The calculation produces 16, and the displayed message can include that result as text.

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

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 like
    value = input()
    Why it fails

    The program pauses, but the user is not given a prompt describing the expected input.

    Do this instead

    Provide a prompt string, such as input("Enter a number: ").

  • Common mistake

    Treating numeric-looking input as an integer automatically.

    What it looks like
    value = input("Enter a number: ")
    Why it fails

    input() returns the entered value as a string, even when the user types numbers.

    Do this instead

    Use 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 like
    print("The result is " + result)
    Why it fails

    The source example converts the result with str() so it can be concatenated with the text in the print() statement.

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

Related concepts

More concepts starting with S