| by Arround The Web | No comments

Python Append to string

In Python, “Strings” are immutable objects, meaning that they cannot be changed once they are defined. However, we can create new strings by adding existing ones together. This process is called “String Concatenation” which appends one string to another. Python provides multiple inbuilt methods for appending a string.

This article presents a thorough guide on how to append one string to another using appropriate examples.

How to Append/Add to a Python String?

To append/add a particular string to another string, the below approaches are utilized in Python:

Method 1: Append to String Using the “+=” Operator

The “+=” operator is used to append one string to another and stores the result to the string that is placed on the left side of the operator.

Example

The following code uses the “+=” operator to append one string to another:

str1 = "Python"
str2 = "Guide"
str1 += str2
print ("Appended String: " + str1)

 

In the above code, the “+=” operator appends the string “str2” to another string “str1”, thereby storing the resultant string to “str1”.

Output

The string has been appended successfully.

Method 2: Append to String Using the “join()” Method

The “join()” method joins the elements of an iterable and returns a string. This method takes a sequence as an argument and joins the elements of the sequence with the specified separator.

Syntax

join(sequence, separator='')

 

In the above syntax, the “sequence” parameter specifies the sequence to be joined and the “separator” parameter specifies the separator to be used between the elements of the sequence. If the separator is not specified, a “space” is used by default.

Example

The below code is used to append one string to another:

str1 = "Python"
str2 = "Guide"
list_value = [str1, str2]
output = " ".join(list_value)
print ("Appended String: ", output)

 

In the above code lines, the “square bracket” notation is used to create a list. The “join()” method takes the list as an argument and appends the list elements(comprising both the strings) based on the specified “ ”.

Output

The given strings have been appended.

Method 3: Append to String Using the “f-String” Method

In Python, an “f-String” is a string literal prefixed with an “f” or “F” and contains expressions inside braces “{}”. It is such that the expressions are substituted with their particular values. The “f-strings” are simpler and more readable when applied to format strings as compared to the “format()” method.

Example

The following code is used to append to a string:

str1 = "Python"
str2 = "Guide"
output = f"{str1} {str2}"
print ("Appended String: ", output)

 

According to the above code, the “f-String” method is used to append the given strings “str1” and “str2” into one another and return a new string.

Output

Method 4: Append to String Using the “string.format()” Method

The “format()” method is used to format strings by taking a series of arguments. These arguments are used to replace the placeholders in the string. The placeholders are denoted by curly braces “{}”. This method is used to append one or multiple strings.

Example

Go through the below-given code:

str1 = "Python"
str2 = "Guide"
output ="{} {}".format(str1, str2)
print ("Appended String: ", output)

 

This code uses the “format()” method that accepts two input strings as its arguments and appends them into an empty string using the curly braces as a placeholder with dot syntax notation.

Output

Method 5: Append to String Using the String “Concatenation Operator”

The string “concatenation operator +” is also used to append one string to another.

Example

Overview of the following code block:

str1 = "Python"
str2 = "Guide"
output = str1 + " " + str2
print ("Appended String: ", output)

 

In this code, the “concatenation operator +” appends the string named “str1” into another string named “str2”.

Output

Conclusion

The “+=” operator, “join()” method, “f-String” method, “string.format()” method, or the “Concatenation Operator +” is used to append/add a particular string to another. The “+=” operator and “+” operators are simple and straightforward ways to append one string to another. The formatting methods such as “f-string”, and “string.format()” can also efficiently append the string in Python. This blog delivered a complete guide on appending to string in Python using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply