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".
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
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".
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".
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
| Operands | Meaning of + | Result |
|---|---|---|
| "2" and "3" | String concatenation | "23" |
| 2 and 3 | Mathematical addition | 5 |
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" + "!"?
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.
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.