Updated April 18, 2023
Introduction to Python string append
In this article, we discuss the concatenation of strings and how to append one string to another string. In python, we can also repeatedly append strings. In Python, concatenation means adding or appending one string to another. Usually, the Python “+” operator is used to add one string to another string by adding one variable to another variable. In Python, we also have append() function, which will add a single item to the existing items. This function will return the modified list of items after applying this append() function to the existing elements. There is also another method known as join(), which is also used for appending strings.
Working of string append with examples
In this article, we will see the append() function in Python and also see other ways of appending or concatenation strings. As we know, the object of the string is immutable; when we use the “+” operator for concatenating strings, a new different string is created. Whereas the appending of the string is just to modify the original string, which is also similar to using the “+” operator. This append function is mostly used on a list of strings. To repeatedly append the strings, we need to first convert it into a list, then append the given elements to that list, and then join the list back to the appended string.
Now let us see how to append a string using the “+” operator, which is also known as a concatenation of strings. Let us see the below example, which uses the “+” operator for concatenating the given string.
str1 = "Educba Training" str2 = " MuMbai, India" print("The given original string : " + str(str1)) print("The string given to append to the previous string: " + str(str2)) res = str1 + str2 print("The Modified string is obtained as follows:") print(res)
In the above program, we have seen that there are two strings, “str1” and “str2,” where str1 is appended with str2 using the “ +“ operator. This is done in the statement “res = str1 + str2,” where we are adding string 1to strin2 to obtain the result, which is stored in the variable “res.” The result is the addition of two strings, or we can say str2 is appended to str1. We can see str2 starts with white space so that when it is appended to the str1, we can read it properly.
Now let us see how to use the append() function to append two strings. This function also works similarly to the “+” operator as above. Let us see the below example using the append() function. This function is mainly used to append more than two strings.
Where item which is to be added to the list of strings.
This will add a single item to the given string or list of strings.
str = ['Educba', 'Training', 'Institute'] print("The given original strings are as follows:") print(str) str.append('Mumbai India') print("Appended string is as follows:") print(str)
In the above program, we have seen that the strings in the list are declared using the “str” variable, and the string that needs to be appended can be directly passed to the append() function. We should note that here we can add only one item to the string list. So to do this to the string list object, we should apply to append() function. Hence this is one way to append strings.
Suppose if we want to append more than one item to the given string list that means adding one string list to another string list can be done as follows:
str1 = ['Educba', 'Training', 'Institute'] print("The first string list is given as follows:") print(str1) str2 = ['Bangalore', 'India'] print("The second string list that needs to be appneded to the first string list is as follows") print(str2) str1.append(str2) print("The modified string list after appending is as follows:") print(str1)
In the above program, we can see we have two string lists where str2 needs to be appended to the str1, and the result is obtained with one single string having the second string appended to it. We can see in the above screenshot that we can see the second string list is appended to the first string-list.
Now let us see another way to append strings using the join() function in Python. Let us consider the below example to demonstrate the join() function. This function is useful when we need to add more strings rather than two strings.
str1 = "Educba Training" str2 = " Mumbai India" print("The given first string : " ) print(str(str1)) print("The given second string that needs to be appended: ") print(str(str2)) res = "".join((str1, str2)) print("The appended string is obtained as follows: ") print(res)
In the above program, we saw another method of appending strings. In the above program, we saw that using the join”() function; we could append the second string to the first string.
In this article, we discussed string append in Python. The string appends itself means concatenation or addition of two or more strings to the given string or string list to obtain the modified string which contains both the strings or lists of strings; in this article first, we saw how to use the “+” operator to append to two strings. Then we saw append() that is used to append string lists with one string or more strings. Lastly, we saw the join() function, which is used to append the strings in which it can also be used to append more than one string.
This is a guide to Python string append. Here we discuss how to use the “+” operator to append to two strings and Working of string append with examples. You may also have a look at the following articles to learn more –