How To Append Or Add String In Python

Strings in Python can’t be changed. You can’t change the original string, but you can always get a new one. You can always get a new string. How can you do this? You can use the += operator, the join() function, f-strings, and space to do this.

Append String In Python

There are many ways to add a string to another string in Python.

  1. Using the += operator to add two strings into one.
  2. String.join() can be used to join two strings.
  3. F-strings are made with Python.

How To Append String Using += Operator In Python

A new string is created when you use the += (plus equal operator) to join two strings together. The original string will not change, but the new string will be. The (+=)operator can be used to do the job of adding something to the end.

#Initializing the String to be Appended
firstString = "This is First String."
secondString = "This needs to be Appended." 

#Printing The Above String
print(firstString)
print(secondString)

#Using += Operator to Append Two String
appendString = ""
appendString += firstString + secondString

#Printing The Appended String.
print(appendString)

Output:

This is First String.
This needs to be Appended.
This is First String.This needs to be Appended.

To begin, we defined two strings, one to which the second string would be appended after the first.
Before concatenating or appending a new string, we printed the two strings. The concatenated string is visible in the final output.

How To Concatenate Multiple String Lines In Python

In Python, you can concatenate multiple string lines at once by using the += operator. If you have three strings named str1, str2, and str3 and want to append them, all you have to do is use str1+str2+str3 and you will have the appended string.

Let’s take a look at the example code below to see how to append multiple strings into one.

#Initializing the String to be Appended
firstString = "This is First String."
secondString = "This needs to be Appended." 
thirdString = "Third String to Append."

#Printing The Above String
print(firstString)
print(secondString)
print(thirdString)

#Using += Operator to Append Two String
appendString = ""
appendString += firstString + secondString + thirdString

#Printing The Appended String.
print(appendString)

Output:

This is First String.
This needs to be Appended.
Third String to Append.
This is First String.This needs to be Appended.Third String to Append.

As you can see in the code above, I was able to append multiple strings at once. You can also divide a given string into multiple strings and then append them to an existing string. Let me show you a code example for multiplying and appending a string.

#Initializing the String to be Appended
firstName = "John"
secondName = "Smith"

#Printing The Above String
print(firstName)
print(secondName)

#If you want to use second Name upto Five Times.
secondName = secondName * 5

#Appending First Name and second Name five times at one.
newString = firstName + secondName

#Printing The Appended String.
print(newString)

Output:

John
Smith
JohnSmithSmithSmithSmithSmith

How To Append String Using Join() Method In Python

To add a string in Python, use the join() method. To do that, you need to make a list and add the strings to the list. Then, use the string join() function to combine them into one long string.

Let us see in the below example code the usage of the Join Method to append two strings in Python.

#Initializing the String to be Appended
firstName = "John"
secondName = "Smith"

#Printing The Above String
print(firstName)
print(secondName)

#Using Join Method to Join Two String
fullName = firstName + " " + "".join(secondName)

#Printing The Appended String.
print(fullName)

Output

John
Smith
John Smith

As you can see in the above code I was able to join the first name and second name into one string named as full name using the join method in Python.

Using f-Strings To Append String In Python

In Python 3.6, f-strings are a new and comprehensive way to format strings. Even more importantly, they’re easier to read and less prone to errors than other formatting methods. They’re also quicker. F-strings can also be used to condense the strings.

#Initializing the String to be Appended
firstName = "John"
secondName = "Smith"

#Printing The Above String
print(firstName)
print(secondName)

#Using Join Method to Join Two String
fullName = f'{firstName} {secondName}'

#Printing The Appended String.
print(fullName)

Output:

John
Smith
John Smith

We appear to be formatting two strings using f-string in this example, but we are actually concatenating them. Finally, this tutorial is over.

How To Append Or Add String In Python

Wrap Up

I hope you’ve learned how to append a string in Python or how to add a string in Python. I’ve listed three methods that you can use: the += operator method, the join() method, and the f-string method.

If you know of a better method than the one discussed above, please let me know in the comments section and I will gladly add it here

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Print A New Line In Python
  2. How To Downgrade Python
  3. Convert String To A List In Python
  4. Python User Input from Keyboard – input() function
  5. Fix: Python Is Not Set From Command Line Or npm Configuration

Leave a Comment