Python append to string - How is it done?
Tags: python, programming, tutorial, beginners
Published: 2022-05-23 05:54:55

Strings in Python are immutable objects, i.e. we can not modify the already existing string but can add a new string to it. It is very easy to append or concatenate strings in Python and here we will talk about a few methods in Python for append to string operation.

### Table of contents

*   [Introduction to Append to strings in Python](#introduction-to-append-to-strings-in-python)
*   [Using += operator to append strings in Python](#using-operator-to-append-strings-in-python)
*   [Using join() to append strings in Python](#using-join-to-append-strings-in-python)
*   [Python string append function](#python-string-append-function)
*   [Using Python f-string](#using-python-fstring)
*   [Closing thoughts](#closing-thoughts)

<span id="section1"></span>  

## Introduction to Append to strings in Python

Whenever we concatenate two strings using "+", we create a new string. However, if we have to append more than two strings, using "+" will create too many temporary strings before getting the desired result.

<span id="section2"></span>

There are other ways like =+ operator, join() function, etc. that can help avoid this.  

## Using += operator to append strings in Python

The plus equal operator (+=) appends to strings and creates a new string while not changing the value of the original string.

#### Input:

    first_name = "Emma"
    second_name = "Watson"

    print ("The first name: " + str(first_name))
    print ("The second name: " + str(second_name))

    first_name += second_name

    print ("The appended string: " + first_name)

<span id="section3"></span>

#### Output:

    The first name: Emma
    The second name: Watson
    The appended string: EmmaWatson 

In the above example, we've combined the two strings, 'Emma' and 'Watson' using +=.

## Using join() to append strings in Python

This function can be used to perform this particular task of concatenating the string. This is more suitable when appending more than two strings together. We create a list and the strings are appended to the list and then using the join() function we merge them into a single string. The following example shows how it's used.

#### Input:

    first_name = "Emma"
    second_name = "Watson"

    print ("The first name: " + str(first_name))
    print ("The second name: " + str(second_name))

    list = [first_name, second_name]
    string = "".join(list)

    print ("The appended string: " + string)

<span id="section4"></span>

#### Output:

    The first name: Emma
    The second name: Watson
    The appended string: EmmaWatson

## Python string append function

In order to append strings multiple times, we can do that by creating a function. The user-defined function created to append the string n times to the already existing string looks like this:

#### Function:

    str = 'Emma'

    def string_append(s, n):     
    	op =  ' '     
    	i = 0
    	while i < n:         
    	  op += s + '-'         
    	  i = i + 1
    	return op   

    jstring = string_append(str, 5) 
    print(jstring)

In this example, the function takes in two parameters, str and no. of times, and then we use a while loop to append strings until the condition is satisfied. It will stop after the condition becomes FALSE.

<span id="section5"></span>

#### Output:

    Emma-Emma-Emma-Emma-Emma-

## Using Python f-string

As of the 3.6 version, Python f-strings is a comprehensive new way to format strings. They are not only more readable, but they are also more concise, less prone to error than other ways of formatting, and faster.

#### Input:

    first_name = "Emma"
    second_name = "Watson"

    print ("The first name: " + str(first_name))
    print ("The second name: " + str(second_name))

    string  = f"{first_name}{second_name}"

    print ("The appended string: " + string)

<span id="section6"></span>

#### Output:

    The first name: Emma
    The second name: Watson
    The appended string: EmmaWatson 

## Closing thoughts

If there are a few strings, then you can use any method to [append](https://www.w3schools.com/python/ref_list_append.asp) them. From a readability perspective, using the += operator seems better for a few strings. However, if you have to append a lot of strings, then you should use the join() function. One can learn about more Python concepts [here](https://flexiple.com/python/python-regex-replace).