04 Ways to Append Strings in Python


When it comes to programming, concatenating strings is one of the most common tasks. We may have a list of Names that we want to combine together into a Full Name, or we may need to add a Salutation to the beginning of each name.

There are a few ways to append strings in python. In this blog post, we will be discussing the + operator, the format method, the Join method, and Python f-strings.

In Python, you can use the += operator to append one string to another string. The += operator in Python is used to append a string to another. It combines the values of two variables together and then stores the final value in one of the variables.

The + Operator

One of the simplest ways to concatenate two strings is by using the + operator. For example, let’s say we have two string variables, str1, and str2:

str1 = "Hello" str2 = "World"
Code language: Python (python)

If we wanted to concatenate these two strings together into a new string, str3, we would simply write:

str3 = str1 + " " + str2 # => "Hello World"
Code language: Python (python)

It’s important to note that when using the + operator, we must explicitly add a space (” “) between each string if we want them to appear as separate words in our new string. Otherwise, they will run together like this:

str3 = str1 + str2 # => HelloWorld # NO SPACE! WON'T PRINT PROPERLY!
Code language: Python (python)

If we want our final string, str3, to appear on multiple lines, we can use the built-in Python method print():

print(str3) # => Hello World
Code language: Python (python)

The print() method will automatically add a newline character (“\n”) after each string it prints. So if you’re concatenating multiple strings together using the + operator and then printing them out with print(), you don’t need to explicitly add any newline characters yourselves. The print() method will take care of that for you.

The format() Method

Another popular way to concatenate strings in Python is by using the built-in format() method. This method allows us to insert values from our program variables into arbitrary places within a string.

For example, let’s say we wanted to create a full name by concatenating a first name and last name stored in separate variables:

first_name = "John" last_name = "Smith" full_name = "{} {}".format(first_name, last_name) # => John Smith
Code language: Python (python)

As you can see from this example, when using the format() method we place curly braces {} where we want our variable values inserted into our final string.

Then, after the closing parenthesis of our format() call, we list out all of the variables whose values we want to be inserted in order of appearance (i.e., first_name followed by last_name).

The Join Method

Yet another way to involve string concatenation is through the use of the built-in join() method.

This method works by taking all of the strings in an iterable and Join – Concatenates them together into one long string with each element separated by whatever character(s) you specify as an argument.

So if wanted to create our full_name variable before using join(), it would look something like this:

first_name = "John" last_name = "Smith" full_name = " ".join([first_name, last_name]) # => John Smith
Code language: Python (python)

Again, notice how here we’re using square brackets [] instead of curly braces {} like we did with format().

That’s because join() requires its argument to be an iterable—something like a list or tuple—rather than just individual variables like format() does.

Python f-strings

Introduced in Python 3.6., f-strings offer several benefits over other methods of string interpolation including easier debugging, support for nested functions, and assignment.

Let’s put f-strings to work and see how they can help us quickly create our full_name variable from before:

first_name = "John" last_name = "Smith" full_name = f"{first_name} {last_name}"# => John Smith
Code language: Python (python)

There are many ways to concatenate strings in Python depending on your preference and what kind of options are available to you given your current development environment constraints (i.e., are you working in Python 2 or Python 3?).

Hopefully, this article has given you some insight into some of your options so that you can make an informed decision about which approach best meets your needs.

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts