In Python, strings are immutable, which means you cannot modify them after creating them. To “append” to a string, you create a new string by combining the original string with the additional content.
Here are four ways to append a string:
- String Concatenation with +
- Using string.join() (Efficient for multiple appends)
- Using the += Operator (Extending string)
- String formatting (Using f-strings or string.format())
Method 1: String Concatenation with +
The + operator explicitly allows us to concatenate or append two strings. It creates a new string each time. This approach is helpful when we need to combine multiple strings into a single expression.
first_name = "Millie" last_name = "Brown" print("The first name: " + str(first_name)) print("The middle name : " + str(last_name)) # Using + operator to append one string to another final_name = first_name + last_name # Printing the result print("The appended string is: " + final_name) # The first name: Millie # The middle name : Brown # The appended string is: MillieBrown
Pros
- The + operator is flexible because it allows us to chain concatenations. e.g., s = a + b + c
- Ideal for quick, one-off appends.
Cons
- Each + creates a new string, leading to O(n²) time complexity for repeated appends.
- It requires writing the variable name twice while coding(e.g., s = s + “x”).
Method 2: Using string.join()
If you are looking for efficient multiple appends, create a list, append the strings to it, and use the string.join() method to merge them to get the final string.
In the above figure, we appended multiple strings into a single final string by converting it into a list and then using the .join() method for efficiency. It appends to a list that is O(1) amortized, making loops O(n).
This approach is especially helpful in building large strings incrementally (e.g., in loops).
first_name = "Millie" middle_name = "Bobby" last_name = "Brown" # Create a list of Strings listOfStrings = [first_name, middle_name, last_name] final_name = "".join(listOfStrings) # print the final result print("The appended string is: " + final_name) # The appended string is: MillieBobbyBrown
Pros
- It appends to a list that is O(1) amortized, making loops O(n).
- It avoids intermediate strings, which can be memory-consuming.
Cons
- It requires us to convert to a list and manage that list.
- It adds complexity to simple cases.
Method 3: Using the += Operator
The += operator creates a new string and assigns it back to the variable. If you want to append a new string, you can do like s += “new part”.
In the above illustration, First, we defined a name, and using the += operator, we appended new names to the original name, and the final string is an appended string.
name = "Millie" name += " Bobby" name += " Brown" print(name) # Millie Bobby Brown
Pros
- It is a simple and concise method.
- It clearly expresses its intention to add a string.
Cons
- Since each time it creates a new string, it leads to O(n²) time complexity for repeated appends.
Method 4: String formatting
Using string formatting, we can embed variables or expressions into a new string.
Using f-strings
As of version 3.6, f-strings provide a comprehensive new method for formatting strings. Our goal is to format strings in a way that results in a concatenated final string.
first_name = "Millie" last_name = "Brown" # use f-strings to append the strings. final_name = f"{first_name} {last_name}" # print result print("The appended string is: " + final_name) # Output: The appended string is: Millie Brown
Using string.format()
The string.format() method is another way of formatting where we use the placeholders to put variables into it, and the final output is a concatenated string.
first_name = "Millie" last_name = "Brown" print("The appended string is: {} {}".format(first_name, last_name)) # Output: The appended string is: Millie Brown
Pros
- It seamlessly integrates variables and expressions.
- The .format() method supports positional and keyword arguments.
- The f-strings approach is highly intuitive for complex templates.
Cons
- The f-strings require Python ≥3.6.
- If no variables are involved, then it can be overkill.
That’s it.