Concepts / String Concatenation and Repetition

String Concatenation and Repetition

The input() function returns a string, so you must use int() to convert it to an integer for arithmetic operations.

  • Programming

From Typed Text to Numeric Data

A program may ask a user to enter a number, but input() always returns what the user typed as a string. If the user types 42, the result is the string '42', not the integer 42. This distinction matters because strings and integers respond differently to operators: strings can be joined or repeated, while integers support arithmetic.

input()'42'int()conversion42integerarithmeticnumeric operationsreturnsproducesenables
What changes when a numeric string returned by input() passes through int(), and how does that enable arithmetic?

What do you think happens?

If input() receives the typed characters 17, what kind of value does it return?

  • The integer 17

  • The string '17'

  • The floating-point number 17.0

Reveal answer
Answer:

The string '17'

input() treats everything the user types as text. Use int() when the text should become an integer.

Using int() Correctly

The int() function converts a string of digits into an integer. The basic form is int(string_value). You can first store the result of input() and then convert it, or place input() directly inside int(). The combined pattern int(input(...)) is useful when you know the user should enter a whole number.

Worked example

Converting a Speed

A user types 17 when asked for a speed. Determine the value after conversion.

  1. 1
    Read the response

    input() captures the typed characters as the string '17'.

  2. 2
    Convert the string

    int('17') produces the integer 17.

  3. 3
    Use the result

    The integer 17 is ready to use in arithmetic expressions.

Result

The converted value is the integer 17, displayed without quotation marks.

ValueTypeMeaning of +Meaning of *
'10'stringjoins textrepeats text
10integerperforms additionperforms multiplication

The quotation marks indicate a string; the unquoted value is an integer.

For example, '10' + '10' produces '1010' because the strings are joined. In contrast, 10 + 10 produces 20 because the integers are added. Likewise, '10' * 3 repeats the string three times, whereas 10 * 3 produces the integer 30. Converting input is therefore essential when typed digits are meant to participate in calculations.

When Conversion Fails

int() can convert a string containing digits and an optional leading minus sign for a negative integer. It cannot convert text that does not represent a valid integer. For example, 'hello' and '12.5' are not valid inputs for int(), so Python raises a ValueError.

user text'hello' or '12.5'integerconversion succeedsvalid integer textdigits or minus signValueErrorconversion failspasses to int()yesno
What happens when int() receives text that does not represent a valid integer?
  • Common mistake

    Assuming that typing digits makes input() return an integer.

    What it looks like

    Treating the response from input() as though it were already the integer 42.

    Why it fails

    input() always returns text, regardless of what the user types.

    Do this instead

    Use int(input(...)) when the response should be a whole number.

  • Common mistake

    Passing decimal text to int().

    What it looks like
    int('12.5')
    Why it fails

    The string does not represent an integer.

    Do this instead

    Recognize that int() raises ValueError for this input.

  • Common mistake

    Passing ordinary words to int().

    What it looks like
    int('hello')
    Why it fails

    The string contains non-numeric characters.

    Do this instead

    Check that the input represents a valid integer before converting it.

Joining Lists with Plus

The + operator concatenates lists. It places the elements of the second list immediately after the elements of the first list, preserving their order. Both operands must be lists, and the operation produces a new list rather than modifying either original list.

[1, 2, 3]first list[1, 2, 3, 4, 5, 6]new list+concatenate[4, 5, 6]second listleft operandjoinsproduces
How do the elements from two input lists move into one new list, and in what order?
Worked example

Combining Two Segments

Combine the lists [1, 2, 3] and [4, 5, 6].

  1. 1
    Place the first list

    The elements 1, 2, and 3 occupy the beginning of the result.

  2. 2
    Append the second list

    The elements 4, 5, and 6 follow immediately after the first three elements.

  3. 3
    Check the result

    The combined list contains six elements in the order supplied by the two operands.

Result

[1, 2, 3, 4, 5, 6]

Repeating Lists with Multiplication

The * operator repeats a list a specified number of times. The right operand is an integer repetition count, and the result is a new list containing the original sequence again and again. The original list is not modified.

[1, 2, 3]one copy[1, 2, 3]copy 1[1, 2, 3, 1, 2, 3, 1,2, 3]three copies[1, 2, 3]copy 2[1, 2, 3]copy 3repeatrepeatrepeatsequencesequencesequence
How does one list become several sequential copies when multiplied by a number?

A one-element list such as [0] repeated four times becomes [0, 0, 0, 0]. A three-element list such as [1, 2, 3] repeated three times becomes [1, 2, 3, 1, 2, 3, 1, 2, 3]. Repetition is useful for creating a pattern or a template with a specified structure.

Reading the Resulting Indices

Concatenation and repetition make predictable index patterns. If the first list has three elements, those elements occupy indices 0 through 2 in the new list. The second list then begins at index 3. For a repeated list, each copy continues the same sequence after the previous copy. Indexing starts at zero, so the position must be counted from the beginning of the resulting list.

011223344556nextnextnextnextnext
After combining or repeating these lists, what element appears at each index?
Worked example

Mapping Values to Positions

For the concatenated list [1, 2, 3] + [4, 5, 6], identify the value at each index.

  1. 1
    Map the first list

    The values 1, 2, and 3 occupy indices 0, 1, and 2.

  2. 2
    Find the starting index of the second list

    Because the first list has three elements, the second list begins at index 3.

  3. 3
    Map the remaining values

    The values 4, 5, and 6 occupy indices 3, 4, and 5.

Result

Index 0 contains 1, index 1 contains 2, index 2 contains 3, index 3 contains 4, index 4 contains 5, and index 5 contains 6.

Original Lists Stay Separate

Both + and * create new list objects. Concatenating two lists does not modify either input list, and repeating a list does not enlarge the original list. The operation produces another list whose contents are based on the operands.

[1, 2, 3]original list[1, 2, 3, 4, 5, 6]new list[4, 5, 6]original listcontributes elementscontributes elements
Which list objects exist before and after the operation, and are the original lists modified?
  • Common mistake

    Expecting list concatenation to change the first list.

    What it looks like

    Assuming that joining two lists automatically enlarges one of the original operands.

    Why it fails

    The + operation creates a new list and leaves the originals unmodified.

    Do this instead

    Use the resulting list as the combined value.

  • Common mistake

    Using a non-list operand with list concatenation.

    What it looks like

    Trying to concatenate a list with a value that is not a list.

    Why it fails

    Concatenation requires both operands to be lists.

    Do this instead

    Make sure both values being joined are lists.

  • Common mistake

    Using a non-integer repetition count.

    What it looks like

    Trying to repeat a list with a value that is not an integer.

    Why it fails

    List repetition requires a list and an integer.

    Do this instead

    Use an integer as the repetition count.

Choosing the Right Operation

GoalOperationResulting behavior
Turn numeric input text into an integerint()Converts valid numeric text into an integer
Join two lists+Creates one new list with the first list followed by the second
Create repeated list patterns*Creates one new list containing sequential copies

Match the operation to the kind of value transformation required.

EASY

Predict the result of each operation: int('17'), '10' + '10', [0] * 4, and [1, 2, 3] + [4, 5, 6]. Then identify which operations create a new list and which operation converts text into an integer.

Hints
  • Remember that quotation marks indicate strings.
  • For list concatenation, place the second list after the first.
  • For list repetition, write the entire list once for each repetition.

Key Takeaways

  1. 1
    input() always returns a string, even when the user types digits.
  2. 2
    int() converts valid integer text into an integer and raises ValueError for invalid integer text.
  3. 3
    Strings and integers have different meanings for + and *.
  4. 4
    List + concatenates lists in order and creates a new list.
  5. 5
    List * repeats a list a specified number of times and creates a new list.
  6. 6
    The original lists remain unchanged, so the resulting values and indices must be traced in the new list.

At a glance

  • input() returns text, so numeric input must be converted with int() before arithmetic.
  • int() accepts valid integer strings and raises ValueError for non-numeric text.
  • The + operator joins lists in order without modifying the original lists.
  • The * operator repeats a list and produces a new list.
  • List indices in the result follow the elements' new sequential positions.

Related concepts

More concepts starting with S