In this article, we will discuss several ways to append one string to another in Python. String is a set of characters similar to C,C++ and other programming languages.
<a id="h-1-quick-examples-of-string-append"></a>
1. Quick Examples of String append
# Consider the two strings
string1 = 'Welcome to'
print("One: ",string1)
string2 = 'Sparkby tutorials!'
print("Two: ",string2)
# Append string2 to string1 using + operator
print("Using + operator:",string1+string2)
# Append string2 to string1 using += operator
string1+=string2
print("Using += operator:",string1)
# Append string2 to string1 using join()
print("Using join():","".join([string1,string2]))
# Append string2 to string1 using join()
print("Using join() with - separator:","-".join([string1,string2]))
# Append string2 to string1 using format()
print("Using format(): ","{} {}".format(string1,string2))
# Append string2 to string1 using f-strings
print(f"Using f-strings: {string1} {string2}")
# Append string2 to string1 using __add__
print("Using __add__:",string1.__add__(string2))
<a id="h-2-string-append-using-operator"></a>
2. String append using + operator
In this scenario, we will append second string to first by joining two strings using + operator. This operator concatenated two strings and return the concatenated string.
<a id="h-2-1-syntax-of-string-operator"></a>
2.1 Syntax of string + operator
The following is the syntax of the String + operator.
string1+string2
# Where string1 is the first string and string2 is the second string.
<a id="h-2-2-example-of-string-operator"></a>
2.2 Example of string + operator
Let's create two strings and append second string to first in a print() statement using + operator.
# Consider the two strings
string1 = 'Welcome to'
print("One: ",string1)
string2 = 'Sparkby tutorials!'
print("Two: ",string2)
# Append string2 to string1 using + operator
print("Using + operator:",string1+string2)
# Append string2 to string1 using + operator with space
print("Using + operator:",string1+" "+string2)
# Output:
# One: Welcome to
# Two: Sparkby tutorials!
# Using + operator: Welcome toSparkby tutorials!
# Using + operator: Welcome to Sparkby tutorials!
Explanation:
-
In second output, we can see that string2 is append to string1 with out any space.
-
In last output, We can see that string2 is appended to a space and space is appended to string1.
<a id="h-3-string-append-using-operator"></a>
3. String append using += operator
In this scenario, we will append second string to first using += operator. So the final string is stored in the first string.
<a id="h-3-1-syntax-of-string-operator"></a>
3.1 Syntax of string += operator
The following is the syntax of the String += operator.
string1+=string2
# Where string1 is the first string and string2 is the second string.
<a id="h-3-2-example-of-string-operator"></a>
3.2 Example of string += operator
Let's create two strings and append second string to first string using += operator.
# Consider the two strings
string1 = 'Welcome to'
print("One: ",string1)
string2 = 'Sparkby tutorials!'
print("Two: ",string2)
# Append string2 to string1 using += operator
string1+=string2
print("Using += operator:",string1)
# Output:
# One: Welcome to
# Two: Sparkby tutorials!
# Using += operator: Welcome toSparkby tutorials!
Explanation:
- So we are appending second string to first using += operator and after printing the first string, we can see that second string is appended to it.
<a id="h-4-string-append-using-join-method"></a>
4. String append using join() method
In this scenario, we will append second string to first using the join() method. This method joins both the strings by a separator. here we need to pass a list as a parameter that takes two strings.
<a id="h-4-1-syntax-of-string-join-method"></a>
4.1 Syntax of string join() method
The following is the syntax of the String join() method.
"separator".join([string1,string2])
# Where string1 is the first string and string2 is the second string.
<a id="h-4-2-string-join-method-parameters"></a>
4.2 String join() method Parameters
It takes list of strings to be joined.
<a id="h-4-3-example-of-string-join-method"></a>
4.3 Example of string join() method.
Let's create two strings and append second string to first string with and without separator using join() method.
# Consider the two strings
string1 = 'Welcome to'
print("One: ",string1)
string2 = 'Sparkby tutorials!'
print("Two: ",string2)
# Append string2 to string1 using join()
print("Using join():","".join([string1,string2]))
# Append string2 to string1 using join()
print("Using join() with - separator:","-".join([string1,string2]))
# Output:
# One: Welcome to
# Two: Sparkby tutorials!
# Using join(): Welcome toSparkby tutorials!
# Using join() with - separator: Welcome to-Sparkby tutorials!
Explanation:
-
In second output, we didn't specified the separator in join() method, so second string is appended to first without any separator.
-
In last output, we specified "-" as the separator in join() method, so second string is appended to first separated by "-".
<a id="h-5-string-append-using-format"></a>
5. String append using format()
In this scenario, we will join the strings using format() method. It is represented by {}. So by placing two {} each other, we will append second string to the first string.
<a id="h-5-1-syntax-of-string-format-method"></a>
5.1 Syntax of string format() method
The following is the syntax of the String format() method.
"{} {}".format(string1,string2)
# Where string1 is the first string and string2 is the second string.
<a id="h-5-2-example-of-string-format"></a>
5.2 Example of string format()
Let's create two strings and append second string to first string using format().
# Consider the two strings
string1 = 'Welcome to'
print("One: ",string1)
string2 = 'Sparkby tutorials!'
print("Two: ",string2)
# Append string2 to string1 using format()
print("Using format(): ","{} {}".format(string1,string2))
# Output:
# One: Welcome to
# Two: Sparkby tutorials!
# Using format(): Welcome to Sparkby tutorials!
<a id="h-6-string-append-using-f-strings"></a>
6. String append using f-strings
In this scenario, we will join the strings using f-strings. It is similar to format(). We need to specify first string and after second string inside {}.
<a id="h-6-1-syntax-of-string-f-strings"></a>
6.1 Syntax of string f-strings
The following is the syntax of the String f-strings.
f"{string1} {string2}"
# Where string1 is the first string and string2 is the second string.
<a id="h-6-2-example-of-string-f-strings"></a>
6.2 Example of string f-strings.
Let's create two strings and append second string to first string using f-strings.
# Consider the two strings
string1 = 'Welcome to'
print("One: ",string1)
string2 = 'Sparkby tutorials!'
print("Two: ",string2)
# Append string2 to string1 using f-strings
print(f"{string1} {string2}")
<a id="h-7-string-append-using-add"></a>
7. String append using add()
In this scenario, we will join the strings using add() method. It is an instance method.
<a id="h-7-1-syntax-of-string-add"></a>
7.1 Syntax of string add()
The following is the syntax of the String add_()
string1.__add__(string2)
# Where string1 is the first string and string2 is the second string.
<a id="h-7-2-example-of-string-add"></a>
7.2 Example of string - add().
Let's create two strings and append second string to first string using add().
# Consider the two strings
string1 = 'Welcome to'
print("One: ",string1)
string2 = 'Sparkby tutorials!'
print("Two: ",string2)
# Append string2 to string1 using __add__
print("Using __add__:",string1.__add__(string2))
<a id="h-8-conclusion"></a>
8. Conclusion
In this article, we seen how to append string with 7 scenarios. In all the scenarios, no module is required. Based on your requirement, You can use any of the method.
<a id="h-related-articles"></a>
Related Articles
- How to Use NumPy clip() in Python
- Python String find() with Examples
- Python String length with Examples
- Python Dictionary setdefault() Method
- Python set union() Function
- Python set difference()
- Python set operators
- NumPy Variance Function in Python
- Python String isnumeric
- Python Convert List of Strings to Ints
- Convert String to Decimal in Python
