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.

returnsproducesenablesinput()'42'int()conversion42integerarithmeticnumeric operations
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.

Converting a Speed

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

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

Convert the string: int('17') produces the integer 17.

Use the result: The integer 17 is ready to use in arithmetic expressions.

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

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

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.

passes to int()yesnouser text'hello' or '12.5'integerconversion succeedsvalid integer textdigits or minus signValueErrorconversion fails
What happens when int() receives text that does not represent a valid integer?
  • Assuming that typing digits makes input() return an integer.

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

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

  • Passing decimal text to int().

    The string does not represent an integer.

    Fix: Recognize that int() raises ValueError for this input.

  • Passing ordinary words to int().

    The string contains non-numeric characters.

    Fix: 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.

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

Combining Two Segments

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

Place the first list: The elements 1, 2, and 3 occupy the beginning of the result.

Append the second list: The elements 4, 5, and 6 follow immediately after the first three elements.

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

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

repeatrepeatrepeatsequencesequencesequence[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 3
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.

nextnextnextnextnext011223344556
After combining or repeating these lists, what element appears at each index?

Mapping Values to Positions

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

Map the first list: The values 1, 2, and 3 occupy indices 0, 1, and 2.

Find the starting index of the second list: Because the first list has three elements, the second list begins at index 3.

Map the remaining values: The values 4, 5, and 6 occupy indices 3, 4, and 5.

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.

contributes elementscontributes elements[1, 2, 3]original list[1, 2, 3, 4, 5, 6]new list[4, 5, 6]original list
Which list objects exist before and after the operation, and are the original lists modified?
  • Expecting list concatenation to change the first list.

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

    Fix: Use the resulting list as the combined value.

  • Using a non-list operand with list concatenation.

    Concatenation requires both operands to be lists.

    Fix: Make sure both values being joined are lists.

  • Using a non-integer repetition count.

    List repetition requires a list and an integer.

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

Key Takeaways

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