How to append a string in python
Adding or appending a string to another string is easy in Python. It is called concatenation and is a basic concept of strings in almost all programming languages.
Ways to Append Strings in Python
We can append a string in two ways:
- Method 1: Using the += operator
There are some separate predefined functions in most of the programming languages to concatenate two strings like concat(). But in Python, we can use the += operator, making the task simpler and easier.
Syntax:
Result string = String1 + string2
Example program:
string1 = "Hello"
string2 = "!"
string3 = "world"
print ("The strings:")
print (string1)
print (string2)
print (string3)
result = string1 + string2 + “ ”+ string3
print ("The concatenated string is: ", result)
Output:
The strings:
Hello
!
world
The concatenated string is: Hello! World
Explanation:
We have declared three strings and concatenated them along with a space to get the resultant sentence.
2. Method 2: Using the join () method
The main purpose of the join () method is to join all the elements in an iterable object ( list, tuple…) into a string with a separating character.
If we have more than 2 strings, it will be hard to keep adding one to the other by the above method. Join () method is feasible in such conditions. We can join any number of strings, and it is a predefined function in the Python libraries.
The unseen mechanism here is that the function creates a new list, and the strings are appended to the list. We use the join () method to merge these strings into one element that gives the final appended string.
Syntax:
“string”. join ([list of strings])
- string: This is the string separating the strings to be concatenated.
- List of strings: All the strings that need to be concatenated are converted into a single list/iterable.
Program:
string1 = "Hello"
string2 = "!"
string3 = "world"
print ("The strings:")
print (string1)
print (string2)
print (string3)
result = " ". join ([string1, string2, string3])
print ("The concatenated string is: ", result)
Output:
The strings:
Hello
!
world
The concatenated string is: Hello! world
Explanation:
We gave the separating string as space. So, we got spaces between the words. Instead of adding strings using the + operator, we can call the join functions with all the strings as a list for multiple strings.
Method 3:
We can write several strings consecutively to concatenate them, but the drawback here is we can't perform this operation in string variables.
Example program:
result = 'ab''cd''e'
print ("The concatenated string is: ", result)
Output:
The concatenated string is abcde
Note: The spaces or backslash characters we give between the strings are not considered. The result will be the concatenated strings
Example:
Result = ‘a’ ‘b’ ‘c’
print (Result)
Output:
abc
- Suppose we want to concatenate an integer or a floating-point number to a string. In that case, we need to convert the data type into the string using the str () function, and then we can go with concatenating because only strings can be concatenated.
Example programs:
string1 = "Hello"
string2 = 56
string3 = "world"
print ("The strings:")
print (string1)
print (string2)
print (string3)
result = string1 + str (string2) + string3
print ("The concatenated string is: ", result)
Output:
The strings:
Hello
56
world
The concatenated string is: Hello56world
Explanation:
In the program, string2 is an integral value. We cannot concatenate an integer to a string. We used the str () function to convert it to a string and then used the concatenation.
There is another way to do this too. We can use the format () function. This function holds place and inserts a specified value into that space. Here is an example.
Program:
string1 = "Hello"
string2 = 56
string3 = "world"
print ("The strings:")
print (string1)
print (string2)
print (string3)
print ("The concatenated string is: {a}{b}{c}".format (a = string1,b = string2,c = string3))
Output:
The strings:
Hello
56
world
The concatenated string is: Hello56world
Explanation:
The integer value – string2 is not converted to a string in the program. Rather, in the print (), we used the format function and used 3 placeholders side by side and inserted string1, string2, and string3.
Using the list append () method
The append () method is defined only for lists. We need to convert the strings into lists when we want to use them.
Program:
string1 = "Hello"
string2 = "world"
print ("The strings:")
print (string1)
print (string2)
list1 = list (string1)
list2 = list (string2)
for i in list2:
list1. append (i)
print ("The concatenated string is:","". join (list1))
Output:
The strings:
Hello
world
The concatenated string is: Helloworld
Explanation:
In above code, we iterated through list2, appended each character into list1, and converted it into a string using the join () function.
It is simply the inner mechanism of the join () method we used above.