Concepts / String Concatenation with the + Operator

String Concatenation with the + Operator

The * operator repeats a string by multiplying it by an integer, producing a new string with the original string concatenated the specified number of times.

  • Programming

Two Meanings of familiar operators

The same operator can behave differently depending on the values around it. With strings, the + operator performs concatenation: it joins strings end to end and creates a new string containing their characters in order. The * operator can repeat a string when the other operand is an integer. Learning to trace the values after each operation makes both behaviors predictable.

What do you think happens?

What is the result of joining the strings "Good" and "morning" with the + operator?

  • Good morning
  • Goodmorning
  • The numbers are added
  • An empty string
Reveal answer

Answer: Goodmorning

The + operator joins the characters exactly as supplied. A space is not added automatically, so the result has no space between the two strings.

Tracing a repeated string

String replication repeats the original string a specified number of times. The multiplier must be an integer. Conceptually, Python starts with an empty result and adds another copy of the original string for each repetition. After five repetitions of the string "*", the result is "*****". This kind of tracing is useful when debugging a separator, pattern, or formatted output.

add one copyadd one copyadd one copyadd one copyadd one copyEmpty result*1 copy**2 copies***3 copies****4 copies*****5 copies
How does the result change step by step when the string "*" is repeated five times?

Building a separator

Predict the result of repeating the string "=" five times.

Identify the original string: The original string contains one equals character.

Identify the multiplier: The integer multiplier is five, so five copies are required.

Place the copies end to end: The five copies form one longer string with five equals characters.

The result is "=====", which can be used as a visual separator.

Joining strings with +

The + operator performs concatenation when used with strings. Concatenation joins the operands end to end to create a new string. The characters from the first operand appear first, followed by the characters from the second operand.

Concatenation is not mathematical addition. The operation does not combine numerical quantities; it produces a string containing the characters from the operands in order. Spaces and other characters are included only when they are explicitly present in one of the strings.

Concatenating several strings

Predict the result of joining "I", " ", and "learn" with the + operator.

Start with the first string: The result begins with the character from the first operand: "I".

Append the second string: The second operand is a separate string containing one space, so the result becomes "I ".

Append the third string: The characters in "learn" are placed after the existing result.

The final result is "I learn".

You can chain the + operator to join more than two strings. Evaluation proceeds from left to right, so each next string is appended after the result built so far. Because spaces are ordinary characters in a string, include a separate space string when the final text needs spacing.

Replication edge cases

repeatrepeatrepeatPositive integer3ababab3 copies of "ab"Zero0Empty stringno copiesNegative integer-2Empty stringno copies
What happens to a string when its repetition count is positive, zero, or negative?

A positive integer produces that many copies of the original string. Repeating a string by zero returns an empty string because zero copies contribute no characters. Repeating a string by a negative integer also returns an empty string. Negative repetition is treated the same as zero repetition.

Concatenation versus addition

concatenateaddString operands"2" + "3"23characters joinedNumeric operands2 + 35values added
What changes when the operands are strings instead of numbers?
Expression kindOperationResult
String operandsConcatenationCharacters appear end to end
Numeric operandsAdditionNumerical values are added

When predicting a + expression, first identify the type of each operand. Then check whether a space or another separator is explicitly included. Finally, read the result from left to right, preserving every character in each string.

Mistakes beginners make

  • Expecting concatenation to insert a space automatically.

    The + operator preserves the characters supplied in the operands; it does not add a space.

    Fix: Include a separate string containing a space between the two words.

  • Treating string concatenation as mathematical addition.

    String concatenation places characters next to one another instead of adding numerical quantities.

    Fix: Identify whether the operands are strings or numbers before predicting the result.

  • Assuming a negative repetition count creates a negative or reversed string.

    Negative repetition is treated the same as zero repetition.

    Fix: Treat zero and negative repetition counts as producing no copies.

  • Using a float as a repetition multiplier.

    String replication requires an integer multiplier, and a float raises a TypeError.

    Fix: Use an integer repetition count.

Practice and application

EASY

Predict each result before checking your reasoning. First, determine the result of joining "red", "-", and "blue" with +. Next, determine the result of repeating "ha" three times. Finally, determine the result of repeating "ha" zero times and then by a negative integer.

Hints
  • For concatenation, preserve the order of the operands and include the hyphen because it is explicitly present.
  • For positive replication, count the required copies.
  • For zero and negative replication, use the empty-string rule.

Checking the predictions

Find the results for the three practice expressions.

Join the color strings: "red" joined with "-" and "blue" produces "red-blue" because the hyphen is an explicit operand.

Repeat three times: Three copies of "ha" placed end to end produce "hahaha".

Apply zero and negative counts: Both zero repetition and negative repetition produce an empty string.

The results are "red-blue", "hahaha", an empty string, and an empty string.

Key takeaways

  1. The + operator concatenates strings by joining their characters end to end.
  2. Concatenation preserves operand order and does not add spaces unless a space is explicitly included.
  3. The + operator performs a different operation with numeric operands: numerical addition.
  4. The * operator repeats a string by an integer and produces the requested number of copies.
  5. Zero or negative repetition produces an empty string, while a float multiplier raises a TypeError.

Key Takeaways

  • Use + with strings to concatenate them in left-to-right order.
  • Include spaces and separators explicitly when they belong in the result.
  • Distinguish string concatenation from numeric addition by checking operand types.
  • Use * with an integer to repeat a string; zero and negative integers produce an empty string.
  • A float cannot be used as the repetition multiplier because it raises a TypeError.