Back to articles list Articles
7 minutes read

How to Append to a String in Python

Find out how to append to a string in Python and concatenate strings in this article. We compare the different string appending methods and discuss which one you should use in each situation.

Strings are one of the most basic data types in Python. But even experienced Python programmers may be confused about how to append text to the end of a string; unlike lists, strings do not have an append() method. Even so, we do have a few other options for appending to a string in Python or even concatenating multiple strings together. In this article, we will explore these options and learn how to append to Python strings.

We assume that you are a bit familiar with Python, its data types, and its basic syntax. If that’s not the case, we suggest you check out our Python Basics track. We even have a dedicated course about working with strings in Python. Otherwise, let’s get started!

What Are Strings in Python?

Let’s start with a quick refresher. Strings are a data type in Python that represent text. They are defined by enclosing a sequence of words in quotation marks. Both single and double quotation marks work – the only requirement is to use the same character on both ends of a string.

Here are some examples of strings and how we can use them to display text on the console:

greeting = "Hello, World!"
name = "Juliano"

print(greeting, "My name is", name)

# output:
# Hello, World! My name is Juliano

Now that we’re on the same page about what strings are, let’s discover how we can append to a string in Python.

Using the Plus Operator to Append to a String in Python

The simplest way to append to a string in python is to use the + (plus) operator. Instead of functioning as a mathematical addition, in Python the + operator is used to concatenate strings together. This means that you can use it to append one string to another.

Here’s an example of the + operator in practice:

first_name = "John"
last_name = "Smith"
full_name = first_name + last_name

print(full_name)

# output:
# JohnSmith

As you can see, we have appended the string "Smith" to the string "John". Also, note how there’s no space between the first and last name. Python does not attempt to "guess" how we might want to append two strings together; it simply concatenates them exactly as they are.

If we want to add a space between the first and last name, we need to do it manually. That’s what we’ve done in the example below:

first_name = "John"
last_name = "Smith"
full_name = first_name + " " + last_name

print(full_name)

# output:
# John Smith

Appending Strings Multiple Times Using the Plus Operator

If you have a list of strings, you can use the + operator to append all of the strings together. In this case, you need to begin with a "template” string, which typically starts empty and is appended to in each iteration.

Here’s how this looks:

names = ["Adam", "Paul", "Steve"]
template = ""

for name in names:
	template = template + name
print(template)

# output:
# AdamPaulSteve

As in our previous example, we need to add spaces manually if we want to separate each word:

names = ["Adam", "Paul", "Steve"]
template = ""

for name in names:
	template = template + name + " "
print(template)

# output:
# Adam Paul Steve

What if we wanted to append strings together in reverse order? One option would be to switch places on the template and name strings, like this:

names = ["Adam", "Paul", "Steve"]
template = ""

for name in names:
	template = name + template
print(template)

# output:
# StevePaulAdam

But that’s not really ideal – the code is a bit difficult to understand at first glance. Instead, you should try to reverse the list of names before iterating over it. We have an article on how to reverse sequences in Python, so check it out if you’re interested!

Appending to Strings vs. Appending to Lists in Python

If you already know about lists and list methods in Python, you are probably familiar with the append() method. It is used to add data to the end of an existing list.

There is one important distinction between appending to strings and appending to lists in Python. Since strings are immutable data types (i.e. the string itself cannot be changed), the + operator always returns a newly-created string. In comparison, appending data to lists with the append() method works in place, since lists are a mutable (i.e. changeable) data type.

The code below exemplifies this distinction:

my_string = "abc"
my_list = ["a", "b", "c"]

new_characters = ["d", "e"]

# Strings are immutable: must write returned value to variable.
for char in new_characters:
	my_string = my_string + char
print(my_string)

# output:
# "abcde"

# Lists are mutable: no need to write returned value to variable.
for char in new_characters:
	my_list.append(char)
print(my_list)

# output:
# ['a', 'b', 'c', 'd', 'e']

Notice how, in the first loop, we needed to capture the output from the operation my_string + char into the same variable my_string. In contrast, we did not need to assign the return value of my_list.append(char), since my_list is modified in place. Keep this detail in mind if you ever wonder why you’re unable to append to a string – you might have forgotten to capture the output to a variable!

If all this talk about lists, loops, and output is going over your head, we recommend checking out our Python Basics track to get up to speed in no time!

Using the join() Method to Concatenate Multiple Strings at Once

In some situations, we may need to append multiple types to a string while inserting a separator between each addition. Suppose we have a list of words to which we want to add more words. We may want to create a new string by adding a comma and a space between each word.

Here’s how we can do this with what we have learned so far:

fruits = ["apple", "orange", "pear"]
final_string = ""

for fruit in fruits:
	final_string = final_string + fruit + ", "

print(final_string)
# output:
# apple, orange, pear,

The code above works... somewhat. The final string contains a trailing comma, from the last iteration of the for loop. We could add some logic to remove the trailing comma in the last operation, but there’s a better way.

We can use the join() method of strings to concatenate the list of strings – all the while dealing with the formatting and edge cases! Here’s the same code as above, but using the join() method instead:

fruits = ["apple", "orange", "pear"]
final_string = ", ".join(fruits)

print(final_string)
# output:
# apple, orange, pear

Success! Not only is the code simpler when using the join() method, it also deals with the trailing character issue for us.

Pay attention to the syntax: we write the separator characters ", " and then call the join() method on them. The list of words is actually an argument to the join() method.

The join() method is preferred whenever we have a list of strings to concatenate and we want a common separator character in between them. If you don’t want a separator character at all, you can always use an empty string, e.g. "".join(fruits).

If your list of strings is very large (tens of thousands of words or more), the join() method is also much faster to compute than repeatedly using the + operator in a for loop. If you’re interested in testing this out for yourself, we have an article on how to evaluate the execution time of your code. Be sure to check it out!

Keep Learning About Python with Us!

That’s it for the article – we hope you now understand the different ways that you can append to a string in Python, as well as the pros and cons of each method.

But don’t stop improving your Python skills. Read how to learn Python beyond the classic 'Hello World!' or try our Python Practice course on Word Games.