Python

Python Append to string

In Python, the strings are immutable. We cannot change the real string. But if we want to concatenate or merge the string that is saved in a variable, we allow the new value to another variable. The original one remains unchanged. In this article, we have discussed appending one string to another. We utilize the “+=” operator, join() method, and f-strings to merge the strings into one another. The “+=’ operator is one of the simple and usual methods to append the string. The “+=” operator concatenates different strings to make a new string.

If we want to add a large number of strings, the “+=” operator makes an unnecessarily large number of temporary strings since the final result is obtained. Let’s explain the approaches of appending one string to another:

Use the “+=” operator to append the string:

In python, the “+=” operator is used to merge the strings. Whatever we want to add numbers or strings, we use this operator. It is used for adding both things. For the implementation of Python codes, we have to install ‘spyder’ software version 5. We create a new project for code by pressing the “new file” option from the menu bar. The name of a new file is “temp.py6”.

In this code, we take two strings named “fname” and “lname”. We assigned values to these strings. “Visual” is assigned to fname. “Programming” is assigned to lname.

The print statement prints both strings. After defining the two strings, we want to add one string to another. For this purpose, we perform the “+=” operator. This operator merges one string, “Visual” to another string “Programming”. Let’s run the code. For running this code, we have to tap the ‘Run’ option from the menu bar of spyder5.

As we use the “+=” operator to append the strings. So the concatenated string is “VisualProgramming”.

Append the string using the join() function:

The use of the join() function is another method of adding different strings. In this instance, we define three strings named string1, string2, and string3. After this, we give values to these three strings. “I” is assigned to the first string. “love” is assigned to the second string. “traveling” is assigned to the third string. Now the print statement prints these strings.

We want to merge these three strings into a single one. So we create a list (listofstrings) that contains the values of all three strings. Then we apply the join() function. This function merges all of the three strings to obtain the value of a final string.

The join() function adds several strings that are present in the list. After passing the join() function, we get the appended string as “Ilovetravelling”.

Append the strings with string formatting:

In python, string formatting is another approach to append the strings. We use f-strings to concatenate the strings. This approach is not only easy to read but it’s also shortened and faster than other formats. Again in this example, we take three strings named string1, string2, and string3. We assigned values to these three strings. “I” is assigned to the first string. “love” is assigned to the second string. “traveling” is assigned to the third string. Now the print statement prints these strings.

We add these three strings with the help of string formatting. Here the values of strings are expanded in the {} that are present in the string. The f-string concatenates these three strings.

The print statement returns the appended string, which is “Ilovetravelling”. Here it shows that we are formatting these three strings using f-string, but literally, we concatenate the strings.

Append the strings ‘n’ times:

We can merge/add the strings numerous times by making another function. In this method, we define the function that adds the string to the original string n times.

In this code, we take only one string that is “programming”. Then we define a function. This function takes two parameters. The string is passed as a first parameter and the second parameter of the string shows how often we want to append the string.

We apply a while loop to merge three strings. This loop goes on and returns the result until the loop reaches the defined number of times “n”. The while loop is used to join a string ‘n’ times.

We take a variable “i” for the while loop. It starts from 1 and returns the string until the loop reaches 8.

The print statement returns eight times the string “programming”. The operator “-” is also printed in between the strings. We also use the while loop in this example. The append() function gives us many concatenated strings.

Conclusion:

We have discussed many ways of appending the strings in Python. We use the “+=” operator, use the join() function, and string formatting to append the strings. If we have a few strings, we use the “+=” operator to merge the string. But if we want to merge more than one string, we use the join() function.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content