Blog Post #17: String Magic: An Introduction to Concatenation

In Post #16, we encountered a surprising behavior. When we tried to add two numbers received from input(), our program produced "57" instead of 12. We learned this happened because the + operator, when used with strings, doesn’t perform mathematical addition—it joins them together. This process has a formal name: concatenation.

In this post, we’ll deliberately explore string concatenation, learning how to use the + operator to combine strings and build dynamic messages from different pieces of text.

What is String Concatenation?

Concatenation is the operation of joining two or more strings together, end-to-end, to create a single, new string. In Python, this is done using the same + symbol we use for addition.

Let’s start with a very simple example using string literals.

result = "Hello" + "World"
print(result)

The output will be:

HelloWorld

Notice that the words are stuck together. Python does exactly what you tell it to do, and we didn’t tell it to add a space. To fix this, we need to explicitly include a space as its own string in the concatenation.

result = "Hello" + " " + "World"
print(result)

Now, the output is what we intended: Hello World.

Concatenating with Variables

String concatenation becomes truly powerful when you use it with variables. It allows you to construct meaningful sentences and messages by combining static text with dynamic data.

Let’s build a user’s full name from two variables.

first_name = "Ada"
last_name = "Lovelace"

# Combine the names with a space string in the middle
full_name = first_name + " " + last_name

print(full_name)

The output will be Ada Lovelace.

We can then use this new full_name variable to build an even more complex string.

sentence = "The full name of the user is " + full_name + "."
print(sentence)

This will print: The full name of the user is Ada Lovelace.

The Big Rule: You Can Only Concatenate Strings

This is the most important rule to remember about string concatenation and a common source of errors for beginners. Python is a strongly-typed language, which means it’s strict about how you mix data types. The + operator will only concatenate a str with another str.

What happens if you try to concatenate a string with a number?

# This will cause a TypeError!
player_name = "Player 1"
score = 100

# Python doesn't know how to add a string and an integer
message = "Score: " + score
print(message)

Running this code will crash your program and show a TypeError: can only concatenate str (not "int") to str. This error is Python’s way of telling you, “I know how to join a string to a string, but I don’t know how to join a string to an integer.”

The solution is to convert the number into a string before you try to concatenate. Just as we used int() and float() in Post #16 to convert strings to numbers, we can use the str() function to convert a number into a string.

player_name = "Player 1"
score = 100

# Convert the integer 'score' to a string before concatenating
message = "Score: " + str(score)

print(message)

Now, the code works perfectly and prints: Score: 100.

What’s Next?

You now know how to concatenate (join) strings using the + operator. You’ve also learned the critical rule that you can only concatenate strings with other strings, and how to use the str() function to convert numbers when you need to include them in a message.

While concatenation with + works, it can become clumsy and hard to read when you have many variables to include. Imagine building a long sentence with many + signs and str() calls! Thankfully, Python has a much more modern and elegant solution. In Post #18, we will learn about f-strings, the preferred way to format strings in modern Python.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment