String Replication with the * Operator
The + operator performs concatenation when used with strings, joining them end to end to create a new string.
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.
Joining Two Strings
Predict the result of the expression "Good" + "morning".
- 1Read the first operand
The first string contributes the characters Good.
- 2Read the second operand
The second string contributes the characters morning.
- 3Place 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
GoodmorningSpaces 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".
"Good" + "morning"Goodmorning
"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.
Following the Intermediate Results
Predict the result of "I" + " " + "learn" + " Python".
- 1First operation
"I" + " " produces the intermediate string "I ". The trailing space is now part of that result.
- 2Second operation
"I " + "learn" produces "I learn".
- 3Third operation
"I learn" + " Python" produces the final string "I learn Python".
I learn Python
Concatenation or Addition
| Operands | Meaning of + | Result |
|---|---|---|
| "2" and "3" | String concatenation | "23" |
| 2 and 3 | Mathematical addition | 5 |
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 failsNeither operand contains a space, so concatenation places world immediately after Hello.
Do this insteadInclude a separate space string: "Hello" + " " + "world".
Common mistake Treating string digits as numbers.
What it looks like"4" + "6"Why it failsThe operands are strings, so the characters are joined rather than mathematically added.
Do this insteadPredict 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 failsThe strings are joined in sequence from left to right, so their character order is preserved.
Do this insteadTrace 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" + "!"?
Reveal 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.
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.