In this article, we will learn to append multiple strings or characters in a python program.
In Python, strings are immutable objects, which means we cannot modify the original string. However, we can create new strings from an existing one in python.
There are various ways to modify and add two or more strings together like using the f-string, join function or the += operator.
Ways to append strings in Python
In Python there are three common ways to append strings :
- Using the += operator
- Using join() function
- Using f-string
Let’s see each of the methods with an example.
Concatenate strings using += operator
We can concatenate two strings into a single string using the +=
operator in python.
Using this operator we can append one string upto another to create the final string.
Example:
str1 = 'Hello '
str2 = 'How are you?'
str1+=str2
print(str1)
Output:
Hello How are you?
Here, we have defined two string variables (str1 and str2) that we want to append to one another.
Then we used the +=
operator to append the second string with the first one and print it out in the terminal.
Using +=
operator we can merge only two strings together. So if want to concatenate multiple strings then we have to use the for loop statement with +=
operator.
Let’s see an example to append string in python using for loop.
str_list = ['hello ', 'my name is', ' Jeff']
append_str = ""
for item in str_list:
append_str += item
print(append_str)
Output:
hello my name is Jeff
In the example, we have a list of string. We then loop
through each string in the list using the for
loop statement.
On each loop, we append each individual string with the other using the +=
operator.
Append string using String.join() function
To append strings together in python we can use the join()
function. It takes in a list or tuple of strings and then using the join()
function it merges all the strings to create a new final string.
The join()
method takes in iterables and then merges them into one string.
Example:
str_list = ["hello","This is me.","How are you?"]
append_str = ' '.join(str_list)
print(append_str)
Output:
hello This is me. How are you?
The empty string (' '
) is the character that acts as a separator between the strings. In the example, we have used a whitespace as a separator.
You can append different characters to strings using the join()
function.
Append string using f-string
In version 3.6, python introduced the new f-string
that is used to format strings in python. It is called the formatted string literals.
It is the easiest way to format strings in python. It is more readable, faster and less prone to errors.
To format a string using f-string literals we have to use f at the beginning of the string and then the curly braces containing the expressions to be replaced by their values.
Now, using this f-string we will append one string with another easily in the example below.
Example:
str1 = 'Hello'
str2 = 'My name is Jeff.'
append_string = f'{str1} {str2}'
print(append_string)
Output:
Hello My name is Jeff.
Using f-string
we can join multiple strings easily into one single string in python.
Conclusion:
In this post, we have learned and seen different methods to append strings in a python program.
Related Topics:
Python – Remove Newline From Strings (3 Ways)
Python – Check if a string is Empty or Not
Replace character in String by index in Python