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.

Goodfirst stringGoodmorningnew stringmorningsecond stringfirst partnext part
How do two string values become one new string when + joins them?
Worked example

Joining Two Strings

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

  1. 1
    Read the first operand

    The first string contributes the characters Good.

  2. 2
    Read the second operand

    The second string contributes the characters morning.

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

Result

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

Before
"Good" + "morning"

Goodmorning

After
"Good" + " " + "morning"

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

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

Following the Intermediate Results

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

  1. 1
    First operation

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

  2. 2
    Second operation

    "I " + "learn" produces "I learn".

  3. 3
    Third operation

    "I learn" + " Python" produces the final string "I learn Python".

Result

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 visible symbols can look similar, but the operation differs according to the values being combined.

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

  • Common mistake

    Expecting a space to appear automatically.

    What it looks like
    "Hello" + "world"
    Why it fails

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

    Do this instead

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

  • Common mistake

    Treating string digits as numbers.

    What it looks like
    "4" + "6"
    Why it fails

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

    Do this instead

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

  • Common mistake

    Ignoring the order of a chained expression.

    What it looks like
    "A" + "B" + "C"
    Why it fails

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

    Do this instead

    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.

Related concepts

More concepts starting with S