Strings in Python are immutable objects. You can not modify the original string, but you can always return a new string. There are various ways, such as using the += operator, join() function, f-strings, and appending the strings with space.
To append a string to another in Python, use the += operator. Python += operator appends a string to another. It adds two values together and assigns the final value to a variable.
There are other approaches you can use to append a string in Python.
- Using string.join() method
- Using Python f-strings
Append string using += operator in Python
If you use +=(plus equal operator) to concatenate two strings, a new string is created, and the original string won’t change. The (+=)operator can be used to perform the append task.
# app.py fname = "Millie" mname = "Bobby" # printing fname string print("The first name: " + str(fname)) # printing mname add string print("The middle name : " + str(mname)) # Using += operator adding one string to another fname += mname # print result print("The concatenated string is: " + fname)Output
The first name: Millie The middle name : Bobby The concatenated string is: MillieBobbyFirst, we defined two strings in which we will append the second string to the first string.
Then we printed the two strings and then used the += operator to concate or append the string. From the final output, you can see the concatenated string.
Append string multiple times in Python
You can append strings multiple times by creating a separate function. Let’s create a user-defined function that will append the string n times to the original string.
Output
Millie-Millie-Millie-Millie-Millie-In this example, we defined a string and a function, which takes two parameters: str and number of times. Then we use a while loop to concatenate the strings until they reached the defined number of times, and it will stop after the condition becomes False.
The function string_append() function returns the multiple concatenated strings.
String join() method to append a string
To append a string in Python, use the string join() method. For that, you have to create a list and append the strings to the list. Then use the string join() function to merge them to get the final string.
# app.py fname = "Millie" mname = "Bobby" # printing fname string print("The first name: " + str(fname)) # printing mname add string print("The middle name : " + str(mname)) # Create a list of Strings listOfStrings = [fname, mname] finalString = "".join(listOfStrings) # print the final result print("The appended string is: " + finalString)Output
The first name: Millie The middle name : Bobby The appended string is: MillieBobbyPython f-strings
As of the 3.6 version, Python f-strings is a comprehensive new way to format strings. Not only are they more readable, but they are also more concise but also less prone to error than other ways of formatting, and also they are faster! You can also concate the strings using f-strings.
Output
The first name: Millie The middle name : Bobby The appended string is: MillieBobbyIn this example, it looks like we are formatting two strings using f-string, but we are actually concatenating two strings. So that is it for this tutorial.
Related posts
Python array append
Python tuple append
Python set append