Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add to a string in Python

Getting to Know Strings in Python

In any programming language, a string is a sequence of characters. In Python, a string is an immutable data type. This means that once a string is created, it cannot be changed. However, this does not mean we cannot add to a string. This might seem contradictory, but let's dive into it a bit more to clear up the confusion.

What Does It Mean to Add to a String?

When we say "add to a string", we mean adding another string to the end of the existing one. This process is also known as concatenation. It's kind of like adding a new car to the end of a train. The existing train (or string) stays the same, but we add something new to the end of it.

In Python, there are several ways to add to a string. Let's explore them one by one.

Using the Plus Sign (+)

The most straightforward way to add to a string is using the plus sign (+). This is the same sign we use for addition in mathematics. However, in Python, when it's used with strings, it acts as a concatenation operator.

str1 = "Hello"
str2 = "World"
result = str1 + " " + str2

print(result)  # This will output: Hello World

In the above example, we defined two strings, str1 and str2. We then used the plus sign (+) to add them together, with a space in between, and stored the result in a new variable.

Using the Join Method

Another way to add to a string in Python is using the join() method. This method is a bit more advanced but also more flexible.

str1 = "Hello"
str2 = "World"
result = " ".join([str1, str2])

print(result)  # This will output: Hello World

In this example, we put the strings we wanted to join in a list (a type of data structure in Python that is used to store multiple items in a single variable), and then used the join() method to concatenate them with a space in between.

Using String Formatting

Python also provides several ways to format strings, which can be used to add to a string. One of these ways is using the format() method.

str1 = "Hello"
str2 = "World"
result = "{} {}".format(str1, str2)

print(result)  # This will output: Hello World

In this example, {} are placeholders that get replaced by the arguments we pass to the format() method.

Using F-Strings

Starting from Python 3.6, we can also use f-strings to add to a string. F-strings are a new way to format strings in Python, and they are quite intuitive and easy to use.

str1 = "Hello"
str2 = "World"
result = f"{str1} {str2}"

print(result)  # This will output: Hello World

In this example, we used an f-string to add str1 and str2 together with a space in between. The expressions inside the curly braces {} get replaced by their values.

Considerations When Adding to a String

Although adding to a string in Python is straightforward, there are a few things to keep in mind:

  • Since strings are immutable, every time we add to a string, Python creates a new string. This is not a problem for small strings, but for large strings, it can lead to performance issues.
  • If we want to add a non-string value to a string, we have to convert it to a string first. We can do this using the str() function.

Conclusion: String It All Together

In Python, though strings are immutable, we are not limited in adding to them. Much like a beadwork artist skillfully adds new beads to a string without changing the existing ones, Python provides us with the tools to extend our strings seamlessly. From the straightforward plus sign (+) to the versatile f-strings, we have many ways to express our creativity.

So keep stringing those characters together, and you'll weave beautiful tapestries of code in no time. After all, in the world of programming, our code is our canvas, and strings are some of the most colorful threads we have at our disposal.