DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Originally published at flexiple.com

Python append to string - How is it done?

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

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.

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)
Enter fullscreen mode Exit fullscreen mode

Output:

The first name: Emma
The second name: Watson
The appended string: EmmaWatson 
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Output:

The first name: Emma
The second name: Watson
The appended string: EmmaWatson
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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.

Output:

Emma-Emma-Emma-Emma-Emma-
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Output:

The first name: Emma
The second name: Watson
The appended string: EmmaWatson
Enter fullscreen mode Exit fullscreen mode




Closing thoughts

If there are a few strings, then you can use any method to append 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.

Top comments (0)