- Syntax
- Example 1: Concatenating Two Strings
- Example 2: Concatenating with a Space
- Example 3: Joining Multiple Strings
- Example 4: Concatenating User Input
- Example 5: Concatenating Numeric Strings
- Example 6: Concatenating Empty Strings
- Example 7: Using += for Concatenation
- Example 8: Building a Sentence
- Example 9: Concatenating Special Characters
- Example 10: Verifying the Result Type
- Common Mistakes
- Best Practices
- Key Points to Remember
String concatenation is the process of joining two or more strings together to form a single string.
Python uses the + operator to concatenate strings.
When two strings are joined using the + operator, Python creates a new string containing the combined contents of both strings. The original strings remain unchanged because strings are immutable.
String concatenation is commonly used to build messages, combine names, create file paths, and generate formatted output.
Syntax #
string1 + string2
Components #
| Component | Description |
|---|---|
string1 |
First string. |
+ |
Concatenation operator. |
string2 |
Second string. |
Example 1: Concatenating Two Strings #
main.py
first = "Hello"
second = "World"
result = first + second
print(result)
Output #
HelloWorld
Explanation #
- The
+operator joins the two strings. - No space is added automatically.
- A new string
"HelloWorld"is created.
Example 2: Concatenating with a Space #
main.py
first = "Hello"
second = "World"
result = first + " " + second
print(result)
Output #
Hello World
Explanation #
- A space is itself a string (
" "). - It is concatenated between the two words.
- The final result becomes
"Hello World".
Example 3: Joining Multiple Strings #
main.py
language = "Python"
version = "3.14"
edition = "Tutorial"
text = language + " " + version + " " + edition
print(text)
Output #
Python 3.14 Tutorial
Explanation #
- Multiple strings can be concatenated in a single expression.
- Each
+operator joins another string. - The result is one complete string.
Example 4: Concatenating User Input #
main.py
first_name = input("Enter first name: ")
last_name = input("Enter last name: ")
full_name = first_name + " " + last_name
print(full_name)
Sample Input #
John
Doe
Output #
John Doe
Explanation #
input()returns strings.- Both input strings are joined with a space.
- The result is stored in
full_name.
Example 5: Concatenating Numeric Strings #
main.py
num1 = "10"
num2 = "20"
print(num1 + num2)
Output #
1020
Explanation #
- Both values are strings.
- Concatenation joins the characters.
- Python does not perform arithmetic addition.
Example 6: Concatenating Empty Strings #
main.py
text = ""
word = "Python"
print(text + word)
Output #
Python
Explanation #
- An empty string contributes no characters.
- The result is simply
"Python".
Example 7: Using += for Concatenation #
main.py
message = "Hello"
message += " World"
print(message)
Output #
Hello World
Explanation #
+=appends another string.- A new string is created and assigned back to
message. - The original string object is replaced.
Example 8: Building a Sentence #
main.py
subject = "Python"
verb = "is"
adjective = "powerful"
sentence = subject + " " + verb + " " + adjective + "."
print(sentence)
Output #
Python is powerful.
Explanation #
- Individual words are joined together.
- Spaces and punctuation are also strings.
- The resulting string forms a complete sentence.
Example 9: Concatenating Special Characters #
main.py
username = "admin"
domain = "@example.com"
email = username + domain
print(email)
Output #
admin@example.com
Explanation #
- Special characters such as
@can be part of strings. - Concatenation joins both parts to create an email address.
Example 10: Verifying the Result Type #
main.py
first = "Py"
second = "thon"
result = first + second
print(result)
print(type(result))
Output #
Python
<class 'str'>
Explanation #
- Concatenation always produces another string.
- The data type of the result is
str.
Common Mistakes #
1. Trying to Concatenate a String and an Integer #
Incorrect
age = 25
print("Age: " + age)
Output #
TypeError: can only concatenate str (not "int") to str
Reason
The + operator can concatenate only strings.
Correct
age = 25
print("Age: " + str(age))
2. Expecting Spaces to Be Added Automatically #
Incorrect
first = "Hello"
second = "World"
print(first + second)
Incorrect Expectation #
Hello World
Actual Output #
HelloWorld
Reason
Python does not automatically insert spaces.
Correct
print(first + " " + second)
3. Confusing Concatenation with Addition #
Incorrect
print("15" + "25")
Incorrect Expectation #
40
Actual Output #
1525
Reason
Both operands are strings, so Python joins them instead of adding their numeric values.
Correct
print(int("15") + int("25"))
4. Assuming Concatenation Modifies the Original String #
Incorrect
text = "Hello"
text + " World"
print(text)
Output #
Hello
Reason
Concatenation returns a new string.
The original string remains unchanged unless the result is assigned.
Correct
text = text + " World"
print(text)
Best Practices #
- Insert spaces explicitly when joining words.
- Convert non-string values using
str()before concatenation. - Use meaningful variable names for readability.
- Assign the concatenated result to a variable if it will be reused.
- For complex formatted strings, consider using f-strings (covered later).
Key Points to Remember #
- String concatenation joins two or more strings.
- The
+operator is used for concatenation. - The
+=operator appends one string to another. - Python does not automatically insert spaces.
- Concatenation creates a new string because strings are immutable.
- Both operands must be strings.
- The result of concatenation is always of type
str.