Strings in Python are immutable objects, that is, we cannot modify the existing string but we can add a new string to it. It is very easy to add or concatenate strings in Python and here we will talk about some methods in Python to append to the string operation.
Table of Contents
Introduction to Appending to Strings in Python Using the += Operator to Append Strings in Python Using
- join()
- Append Python String Function
- Using the Python f-string
- Final thoughts
- Introduction to Appending to
- in Python
to Append Strings in Python
strings
Whenever we concatenate two strings using “+”, we create a new string. However, if we have to add 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 the += operator to
append strings in Python
The
most equal operator (+=) is appended to the strings and creates a new string without changing the value of the original string.
Input
: first_name = “Emma” second_name = “Watson” print (“First name: ” + str(first_name)) print (“Middle name: ” + str(second_name)) first_name += second_name print (“The attached string: ” + first_name) Output: First name: Emma Middle name: Watson The appended string
:
EmmaWatson
In the example above, we have 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 best suited when more than two strings are added 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 to use it.
Input
: first_name = “Emma” second_name = “Watson” print (“First name: ” + str(first_name)) print (“Middle name: ” + str(second_name)) list = [first_name, second_name] string = “”.join(list) print (“The appended string: ” + string)
Output
: First name: Emma Middle name: Watson The appended string: EmmaWatson
Function
append Python string To add strings multiple times, we can do this by creating a function. The user-defined function created to append the string n times to the 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 two parameters, str and no. of times, and then we use a while loop to add strings until the condition is met. It will stop after the condition becomes FALSE.
Exit:
Emma-Emma-Emma-Emma-Using the Python f
string
Starting with version 3.6, Python f strings are a complete new way to format strings. Not only are they more readable, but they’re also more concise, less error-prone than other forms of formatting, and faster.
Input
: first_name = “Emma” second_name = “Watson” print (“First name: ” + str(first_name)) print (“Middle name: ” + str(second_name)) string = f”{first_name}{second_name}” print (“The attached string: ” + string) Output: First name: Emma Middle name: Watson The attached string
:
EmmaWatson
Reflections
If there are some strings, you can use any method to append them. From a readability perspective, using the += operator seems better for some strings. However, if you have to add a lot of strings, you must use the join() function. One can learn about more Python concepts here.