String Concatenation and Repetition
The input() function returns a string, so you must use int() to convert it to an integer for arithmetic operations.
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.
What do you think happens?
If input() receives the typed characters 17, what kind of value does it return?
Reveal 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.
- 1Read the response
input()captures the typed characters as the string '17'. - 2Convert the string
int('17')produces the integer 17. - 3Use the result
The integer 17 is ready to use in arithmetic expressions.
The converted value is the integer 17, displayed without quotation marks.
| Value | Type | Meaning of + | Meaning of * |
|---|---|---|---|
| '10' | string | joins text | repeats text |
| 10 | integer | performs addition | performs 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.
Common mistake Assuming that typing digits makes input() return an integer.
What it looks likeTreating the response from input() as though it were already the integer 42.
Why it failsinput()always returns text, regardless of what the user types.Do this insteadUse int(input(...)) when the response should be a whole number.
Common mistake Passing decimal text to int().
What it looks likeint('12.5')Why it failsThe string does not represent an integer.
Do this insteadRecognize that int() raises ValueError for this input.
Common mistake Passing ordinary words to int().
What it looks likeint('hello')Why it failsThe string contains non-numeric characters.
Do this insteadCheck 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.
Combining Two Segments
Combine the lists [1, 2, 3] and [4, 5, 6].
- 1Place the first list
The elements 1, 2, and 3 occupy the beginning of the result.
- 2Append the second list
The elements 4, 5, and 6 follow immediately after the first three elements.
- 3Check 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.
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.
Mapping Values to Positions
For the concatenated list [1, 2, 3] + [4, 5, 6], identify the value at each index.
- 1Map the first list
The values 1, 2, and 3 occupy indices 0, 1, and 2.
- 2Find the starting index of the second list
Because the first list has three elements, the second list begins at index 3.
- 3Map 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.
Common mistake Expecting list concatenation to change the first list.
What it looks likeAssuming that joining two lists automatically enlarges one of the original operands.
Why it failsThe + operation creates a new list and leaves the originals unmodified.
Do this insteadUse the resulting list as the combined value.
Common mistake Using a non-list operand with list concatenation.
What it looks likeTrying to concatenate a list with a value that is not a list.
Why it failsConcatenation requires both operands to be lists.
Do this insteadMake sure both values being joined are lists.
Common mistake Using a non-integer repetition count.
What it looks likeTrying to repeat a list with a value that is not an integer.
Why it failsList repetition requires a list and an integer.
Do this insteadUse an integer as the repetition count.
Choosing the Right Operation
| Goal | Operation | Resulting behavior |
|---|---|---|
| Turn numeric input text into an integer | int() | 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.
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 raisesValueErrorfor invalid integer text. - 3Strings and integers have different meanings for + and *.
- 4List + concatenates lists in order and creates a new list.
- 5List * repeats a list a specified number of times and creates a new list.
- 6The 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 withint()before arithmetic.int()accepts valid integer strings and raisesValueErrorfor 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.