How to Append One String to Another in Python?

In the Python programming language, strings are the sequences of specific characters. The names of things, people, and places are considered as a string. Strings cannot be changed or modified once they are created/defined. However, users are allowed to append/add one string to another existing one in Python. To append/concatenate strings means to join or add a particular string’s values to another string. Python provides numerous methods to perform this operation. 

Quick Outline

The following content will be covered to append one string to another in Python:

  1. How to Create Strings in Python?
  2. How to Append/Add One Python String to Another?
  3. Method 1: Append/Add One Python String to Another Using “+” Operator
  4. Method 2: Append/Add One Python String to Another Using “join()” Method
  5. Method 3: Append/Add One Python String to Another Using “format()” Method
  6. Method 4: Append/Add One Python String to Another Using f-strings
  7. Method 5: Append/Add One Python String to Another Using “__add__” Method
  8. Method 6: Append/Add One Python String to Another Using “+=” Operator
  9. Method 7: Append/Add One Python String to Another Using “StringIO” Method
  10. How to Handle Exceptions While Appending Strings in Python?

How to Create Strings in Python?

To create strings in Python, users need to put the sequence of the desired characters inside the single quotes or double quotes and assign it to a specific variable. 

For example, we have the following characters i.e. “Its Linux Foss”. We will enclose them within single quotes, assign them to a “string1” variable, and display the result:

string1 = 'Its Linux Foss'

print(string1)

By doing so, the string has been created and printed as seen below:

Similarly, users can use the double quotes to create a string. Here, we have the same characters but we will enclose them within double quotes, assign them to a “string2” variable, and display the result:

string2 = "Its Linux Foss"

print(string2)

This will give the following output:

Since the strings have been created, now let’s see how we can append one string to another in Python. 

How to Append/Add One Python String to Another?

Appending a string with another string means adding one string at the end of any other string. This can be done in Python using different built-in functions and operators such as the “+” operator, “join()” method, “format()” method, etc.

Method 1: Append/Add One Python String to Another Using “+” Operator

The “+” operator is used to append/add one string to another desired string in Python. This operator enables users to concatenate/join two or more strings together. This is an easy-to-use and great option to join two strings. However, if users want to join multiple strings, it leads to slower performance. 

Here, we have three strings stored in the “str1”, “str2” and “str3” variables. We will append/concatenate these strings using the “+” operator, store their result in the “res” variable, and print them:

str1 = 'Its'
str2 = 'Linux'
str3 = 'Foss'

res = str1 + str2 + str3
print("Appended String: ", res)

By doing so, the strings have been appended/added successfully:

In the above output, it can be observed that the value of three strings has joined together without any spacing i.e. “ItsLinuxFoss”. To add space between these strings, users are required to do it manually by adding the space after the first string value or before the second string value. Look at the following code to understand it better:

str1 = 'Its '
str2 = 'Linux '
str3 = 'Foss'

res = str1 + str2 + str3
print("Appended String: ", res)

As you can see, this has added the strings with space between them:

Alternatively, users can use the string literal (quotation marks) between string variables to add the space between them: 

str1 = 'Its'
str2 = 'Linux'
str3 = 'Foss'

res = str1 + " " + str2 + " " + str3
print("Appended String: ", res)

This will give the following output:

Method 2: Append/Add One Python String to Another Using “join()” Method

The “join()” method is the most efficient method to append/join strings in Python. It is useful in the case of appending/joining multiple strings together. This method takes iterable objects, such as strings, dictionaries, lists, sets, tuples, etc. as an argument and joins them.

For instance, we are joining three strings i.e. “str1”, “str2” and “str3” using the “join()” method, storing the result in the “res” variable and displaying it:

str1 = 'Its'
str2 = ' Linux'
str3 = ' Foss'

res = ''.join([str1, str2, str3])
print("Appended String: ", res)

The below screenshot shows the appended string:

Method 3: Append/Add One Python String to Another Using “format()” Method

The “format()” method can also be used to append one or multiple strings in Python. This method takes string variables as arguments, inserts them into a specific placeholder (curly brackets) “{}” and returns a string. It enables users to manage where and how the string value should be inserted into another string.

Here, we are appending “str3” and “str2” to “str1” using the “format()” method and “{}” placeholders:

str1 = 'Its'
str2 = 'Linux'
str3 = 'Foss'

res = '{} {} {}'.format(str1, str2, str3)
print("Appended String: ", res)

In the below output it can be observed that the strings have been appended successfully:

Method 4: Append/Add One Python String to Another Using f-strings

The “f-strings” offer a convenient way to append strings. It contains expressions within the curly braces “{}” that are evaluated and their values are added/inserted into the string. It also enables users to manage complex string manipulations.

For example, we are appending “str3” and “str2” to “str1” using the “f-strings” method. The result will be stored in the “res” variable and will be displayed: 

str1 = 'Its'
str2 = 'Linux'
str3 = 'Foss'

res = f"{str1} {str2} {str3}"
print("Appended String: ", res)

This method has appended the strings successfully as seen below:

Method 5: Append/Add One Python String to Another Using “__add__” Method

The “__add__” method is a special method that is used to add two objects in Python. Users can utilize this method to add/join two or more strings.

Here, we are adding “str1”, “str2” and “str3” using the “__add__” method as seen below:

str1 = 'Its'
str2 = ' Linux'
str3 = ' Foss'

res = str1.__add__(str2).__add__(str3)
print("Appended String: ", res)

The below output indicates that the strings have been appended:

Method 6: Append/Add One Python String to Another Using “+=” Operator

The “+=” operator is an addition assignment operator that adds the values of two variables and assigns the result to the left variable. This operator can also be used to append one string to another and store the result on its left side variable. 

For instance, we are appending the “str3” string to the “str2” string and the result will be stored in the “str2” string. Then, appending “str2” to “str1” and storing the result in “str1”. Finally, displaying the “str1” string:

str1 = 'Its'
str2 = ' Linux'
str3 = ' Foss'

str2 += str3
str1 += str2
print ("Appended String: ", str1)

According to the below output, the strings have been appended:

Method 7: Append/Add One Python String to Another Using “StringIO” Module

The “StringIO” is a Python module that enables users to manage or work with strings like they are files. It offers a file-like interface where users can write and read strings in memory. It can be used to append/add one string to another Python string.

To do so, import the “StringIO” module. Then, create an instance of “StringIO” and allocate it to a “concat_str” variable. After that, use the “write()” method to write the desired strings to the “StringIO” object. Finally, get the appended string using the “getvalue()” method. 

Here, we have appended the following strings:

from io import StringIO

concat_str = StringIO()

concat_str.write('Its ')
concat_str.write('Linux ')
concat_str.write('Foss')

print("Appended String: ", concat_str.getvalue())

It can be seen that the string values have been written and joined together successfully:

How to Handle Exceptions While Appending Strings in Python?

Appending/joining strings in Python is not a difficult task. However, some users may encounter various issues while performing this operation. Let’s discuss some issues that can be encountered while appending strings in Python and check their possible solutions.

Case 1: Appending Non-string Type to String

The common issue while appending strings in Python is adding a non-string type to a string. Go through the example below to understand it better.

In this example, we are trying to append/add a specific number “num” to a string “my_str”. It showed a TypeError because this is not allowed in Python:

To resolve this error, use the “str()” function to convert the desired number to a string as seen below:

my_str = 'World'
num = 7

res = my_str + str(num)

print("Appended String: ", res)

This has successfully appended the specified number to the string:

Case 2: Appending Unicode String to Byte String

Another common problem occurs while appending the Unicode string to a particular byte string. Look at the given example to gain a better understanding.

In this example, we are appending a Unicode string “uni_str” to a byte string “byt_str”. This displayed the error because Python does not allow it:

To fix this problem, it is required to use the “decode()” method to decode the specified byte string to a Unicode string:

byt_str = b'Linux'
uni_str = ' Foss'

res = byt_str.decode() + uni_str

print("Appended String: ", res)

It can be seen that the Unicode string has been successfully appended to the byte string:

Note: Click on the provided link to access Google Colab. 

Conclusion

To append/add one Python string to another, different methods are used. Users can add/join two or multiple strings using the “+” operator, “join()” method, “format()” method, “__add__” method, “f-strings”, “+=” operator, or “StringIO” module. Using the “+” operator is the simple and easiest way to add/join two strings. In the case of appending multiple strings, the “join(), “format()” and “f-strings” methods are the most efficient. This blog has demonstrated the methods of creating strings, appending one string to another, and the way to handle exceptions while appending strings in Python.