Concepts / String Replication with the * Operator

String Replication with the * Operator

The + operator performs concatenation when used with strings, joining them end to end to create a new string.

  • Programming

A Different Meaning for +

The supplied section title refers to string replication with the * operator, but the concept described in the source material is string concatenation with the + operator. In this article, the focus is therefore the documented behavior of +: when it is used with strings, it joins those strings end to end and produces a new string.

Joining Two Values

String concatenation places every character from the first string before every character from the second string. The result is one new string, and the original order of the characters is preserved. Nothing is inserted automatically between the two operands.

first partnext partGoodfirst stringGoodmorningnew stringmorningsecond string
How do two string values become one new string when + joins them?

Joining Two Strings

Predict the result of the expression "Good" + "morning".

Read the first operand: The first string contributes the characters Good.

Read the second operand: The second string contributes the characters morning.

Place the strings together: The second string is placed immediately after the first. No space is added because no space character appears in either operand.

Goodmorning

Output
Goodmorning

Spaces Are Characters

Concatenation does not format the result for you. If the final text should contain a space, that space must be included explicitly as part of one of the strings. Compare joining "Good" and "morning" directly with joining "Good", " ", and "morning".

Chaining Several Strings

The + operator can join more than two strings. In a chain, evaluation proceeds from left to right. The first two strings form an intermediate result; that intermediate result is then joined with the next string, continuing until the expression is complete.

then jointhen join"I" + " ""I ""I " + "learn""I learn""I learn" + " Python""I learn Python"
In what order are several strings joined, and what does the intermediate result contain after each + operation?

Following the Intermediate Results

Predict the result of "I" + " " + "learn" + " Python".

First operation: "I" + " " produces the intermediate string "I ". The trailing space is now part of that result.

Second operation: "I " + "learn" produces "I learn".

Third operation: "I learn" + " Python" produces the final string "I learn Python".

I learn Python

Concatenation or Addition

++++"2"string2number"3"string3number"23"joined characters5mathematical result
What changes when + combines strings instead of numbers?
OperandsMeaning of +Result
"2" and "3"String concatenation"23"
2 and 3Mathematical addition5

The quotation marks matter in these examples. "2" and "3" are strings, so + joins their characters to produce "23". The unquoted values 2 and 3 are numbers, so + represents mathematical addition and produces 5. The string result is not the numerical sum of the characters or digits; it is a new string containing the operands in order.

Mistakes to Check

  • Expecting a space to appear automatically.

    Neither operand contains a space, so concatenation places world immediately after Hello.

    Fix: Include a separate space string: "Hello" + " " + "world".

  • Treating string digits as numbers.

    The operands are strings, so the characters are joined rather than mathematically added.

    Fix: Predict the string result "46", not the numerical result 10.

  • Ignoring the order of a chained expression.

    The strings are joined in sequence from left to right, so their character order is preserved.

    Fix: Trace the intermediate result: "A" + "B" becomes "AB", then "AB" + "C" becomes "ABC".

Predict Before Evaluating

What do you think happens?

What exact string results from "Tea" + " " + "time" + "!"?

  • Teatime!
  • Tea time!
  • Tea time
  • Tea! time
Reveal answer

Answer: Tea time!

The first + joins Tea with the explicit space, the next joins time after that space, and the final + places ! at the end. The characters remain in left-to-right order.

EASY

Predict each result before checking your answer: "red" + "blue"; "red" + " " + "blue"; "1" + "2" + "3". For each one, write every character in the final string, including spaces.

Hints
  • Look for an explicit space string.
  • Keep the operands in their written order.
  • Quoted digits remain characters in a string concatenation.

Key Takeaways

  • With strings, + performs concatenation rather than mathematical addition.
  • Concatenation joins all characters from the operands in their existing order and creates a new string.
  • Spaces must be included explicitly as characters in one of the strings.
  • A chain of + operations is evaluated from left to right, producing intermediate strings along the way.
  • Quoted digits are strings, while unquoted numerical values participate in mathematical addition.