String Concatenation Using Plus
🏷️ Python Basics: Syntax, Variables, and Types / String Basics
When working with text data in Python, you'll often need to combine multiple strings together. This process is called string concatenation, and the simplest way to do it is using the plus (+) operator. Think of it like gluing pieces of text together to form a complete sentence or message.
⚙️ What Is String Concatenation?
String concatenation means joining two or more strings end-to-end to create a single, longer string. The plus operator (+) acts as the "glue" between your string values.
- Basic idea: Take "Hello" + " " + "World" to get "Hello World"
- The plus operator works only between strings — if you try to mix types, Python will give you an error
- You can concatenate as many strings as you need in a single expression
🛠️ How to Concatenate Strings with Plus
Using the plus operator is straightforward. You place the + symbol between the string values you want to join.
Simple example: - Write: first_name = "John" and last_name = "Doe" - Then: full_name = first_name + " " + last_name - Result: full_name now holds "John Doe"
Concatenating literals and variables: - Write: greeting = "Hello, " + "friend!" - Result: greeting becomes "Hello, friend!" - You can mix string variables and literal strings (text in quotes) freely
Multiple concatenations:
- Write: message = "The " + "quick " + "brown " + "fox"
- Result: message becomes "The quick brown fox"
- Each + adds the next piece to the growing string
📊 Common Use Cases
| Use Case | Example Code | Result |
|---|---|---|
| Building a name | "Mr. " + "Smith" | "Mr. Smith" |
| Creating a file path | "/home/" + "user" + "/docs" | "/home/user/docs" |
| Forming a sentence | "Error: " + "file not found" | "Error: file not found" |
| Combining user input | name = "Alice" then "Welcome, " + name | "Welcome, Alice" |
🕵️ Important Rules to Remember
- Strings only: You cannot concatenate a string with a number directly. Writing "Age: " + 30 will cause an error
- Fix the error: Convert the number to a string first using str() — for example: "Age: " + str(30)
- Spaces matter: Python does not add spaces automatically. You must include spaces inside your strings or as separate string pieces
- Order matters: The result follows the exact order you write the strings — left to right
Correct example with a number: - Write: age = 25 - Write: message = "You are " + str(age) + " years old" - Result: message becomes "You are 25 years old"
🎯 Practical Example: Building a Status Message
Imagine you need to create a status message that includes a server name and its current state.
- Write: server = "web-01"
- Write: status = "running"
- Write: report = "Server " + server + " is " + status
- Result: report becomes "Server web-01 is running"
If the status changes, you only need to update the status variable, and the concatenation will rebuild the message automatically.
✅ Key Takeaways
- The plus operator (
+) is the simplest way to join strings in Python - Always ensure both sides of
+are strings — use str() to convert numbers - Remember to include spaces manually where needed
- String concatenation works with variables, literal text, or a mix of both
- This method is great for building simple messages, paths, or combining user input
String concatenation with plus is your first tool for working with text — it's simple, readable, and perfect for getting started with Python strings.
String concatenation using the plus operator joins two or more strings together into one combined string.
➕ Example 1: Joining two simple strings
This example shows the most basic way to combine two strings with the plus operator.
first_name = "Jane"
last_name = "Smith"
full_name = first_name + last_name
print(full_name)
📤 Output: JaneSmith
➕ Example 2: Adding a space between strings
This example demonstrates that the plus operator does not add spaces — you must include them manually.
city = "New York"
state = "NY"
location = city + ", " + state
print(location)
📤 Output: New York, NY
➕ Example 3: Concatenating string variables with literal strings
This example shows how to mix stored variables with direct string values using the plus operator.
product = "laptop"
message = "Your " + product + " has shipped."
print(message)
📤 Output: Your laptop has shipped.
➕ Example 4: Building a sentence from multiple parts
This example combines several string variables to form a complete sentence.
subject = "The sensor"
action = "recorded"
value = "42.5 degrees"
sentence = subject + " " + action + " " + value + "."
print(sentence)
📤 Output: The sensor recorded 42.5 degrees.
➕ Example 5: Concatenating strings in a loop for a report
This example builds a multi-line report by joining strings inside a loop — a common pattern for engineers.
readings = ["12.3", "14.7", "11.9"]
report = "Temperature Readings:\n"
for reading in readings:
report = report + "- " + reading + "°C\n"
print(report)
📤 Output: Temperature Readings:\n- 12.3°C\n- 14.7°C\n- 11.9°C\n
Comparison: String Concatenation Using Plus
| Feature | Behavior |
|---|---|
| Joins strings | Yes, exactly as written |
| Adds spaces automatically | No — you must add them |
| Works with variables | Yes |
| Works with literal strings | Yes |
| Can combine in loops | Yes — builds strings incrementally |
When working with text data in Python, you'll often need to combine multiple strings together. This process is called string concatenation, and the simplest way to do it is using the plus (+) operator. Think of it like gluing pieces of text together to form a complete sentence or message.
⚙️ What Is String Concatenation?
String concatenation means joining two or more strings end-to-end to create a single, longer string. The plus operator (+) acts as the "glue" between your string values.
- Basic idea: Take "Hello" + " " + "World" to get "Hello World"
- The plus operator works only between strings — if you try to mix types, Python will give you an error
- You can concatenate as many strings as you need in a single expression
🛠️ How to Concatenate Strings with Plus
Using the plus operator is straightforward. You place the + symbol between the string values you want to join.
Simple example: - Write: first_name = "John" and last_name = "Doe" - Then: full_name = first_name + " " + last_name - Result: full_name now holds "John Doe"
Concatenating literals and variables: - Write: greeting = "Hello, " + "friend!" - Result: greeting becomes "Hello, friend!" - You can mix string variables and literal strings (text in quotes) freely
Multiple concatenations:
- Write: message = "The " + "quick " + "brown " + "fox"
- Result: message becomes "The quick brown fox"
- Each + adds the next piece to the growing string
📊 Common Use Cases
| Use Case | Example Code | Result |
|---|---|---|
| Building a name | "Mr. " + "Smith" | "Mr. Smith" |
| Creating a file path | "/home/" + "user" + "/docs" | "/home/user/docs" |
| Forming a sentence | "Error: " + "file not found" | "Error: file not found" |
| Combining user input | name = "Alice" then "Welcome, " + name | "Welcome, Alice" |
🕵️ Important Rules to Remember
- Strings only: You cannot concatenate a string with a number directly. Writing "Age: " + 30 will cause an error
- Fix the error: Convert the number to a string first using str() — for example: "Age: " + str(30)
- Spaces matter: Python does not add spaces automatically. You must include spaces inside your strings or as separate string pieces
- Order matters: The result follows the exact order you write the strings — left to right
Correct example with a number: - Write: age = 25 - Write: message = "You are " + str(age) + " years old" - Result: message becomes "You are 25 years old"
🎯 Practical Example: Building a Status Message
Imagine you need to create a status message that includes a server name and its current state.
- Write: server = "web-01"
- Write: status = "running"
- Write: report = "Server " + server + " is " + status
- Result: report becomes "Server web-01 is running"
If the status changes, you only need to update the status variable, and the concatenation will rebuild the message automatically.
✅ Key Takeaways
- The plus operator (
+) is the simplest way to join strings in Python - Always ensure both sides of
+are strings — use str() to convert numbers - Remember to include spaces manually where needed
- String concatenation works with variables, literal text, or a mix of both
- This method is great for building simple messages, paths, or combining user input
String concatenation with plus is your first tool for working with text — it's simple, readable, and perfect for getting started with Python strings.
Interactive Views
You are currently in 📚 All-in-One mode. Use the tabs at the top to switch to 📖 Theory Only or 💻 Code Only views.
String concatenation using the plus operator joins two or more strings together into one combined string.
➕ Example 1: Joining two simple strings
This example shows the most basic way to combine two strings with the plus operator.
first_name = "Jane"
last_name = "Smith"
full_name = first_name + last_name
print(full_name)
📤 Output: JaneSmith
➕ Example 2: Adding a space between strings
This example demonstrates that the plus operator does not add spaces — you must include them manually.
city = "New York"
state = "NY"
location = city + ", " + state
print(location)
📤 Output: New York, NY
➕ Example 3: Concatenating string variables with literal strings
This example shows how to mix stored variables with direct string values using the plus operator.
product = "laptop"
message = "Your " + product + " has shipped."
print(message)
📤 Output: Your laptop has shipped.
➕ Example 4: Building a sentence from multiple parts
This example combines several string variables to form a complete sentence.
subject = "The sensor"
action = "recorded"
value = "42.5 degrees"
sentence = subject + " " + action + " " + value + "."
print(sentence)
📤 Output: The sensor recorded 42.5 degrees.
➕ Example 5: Concatenating strings in a loop for a report
This example builds a multi-line report by joining strings inside a loop — a common pattern for engineers.
readings = ["12.3", "14.7", "11.9"]
report = "Temperature Readings:\n"
for reading in readings:
report = report + "- " + reading + "°C\n"
print(report)
📤 Output: Temperature Readings:\n- 12.3°C\n- 14.7°C\n- 11.9°C\n
Comparison: String Concatenation Using Plus
| Feature | Behavior |
|---|---|
| Joins strings | Yes, exactly as written |
| Adds spaces automatically | No — you must add them |
| Works with variables | Yes |
| Works with literal strings | Yes |
| Can combine in loops | Yes — builds strings incrementally |