You are currently viewing Python String append with Examples

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.

Advertisements

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))

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.

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.

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:

  1. In second output, we can see that string2 is append to string1 with out any space.

2. In last output, We can see that string2 is appended to a space and space is appended to string1.

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.

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.

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:

  1. 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.

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.

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.

4.2 String join() method Parameters

It takes list of strings to be joined.

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:

  1. In second output, we didn’t specified the separator in join() method, so second string is appended to first without any separator.

2. In last output, we specified “-” as the separator in join() method, so second string is appended to first separated by “-“.

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.

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.

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!

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 {}.

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.

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}")

7. String append using __add__()

In this scenario, we will join the strings using __add__() method. It is an instance method.

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.

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))

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.