Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Append a String to Another in Python

  • 04 Feb, 2025
  • Com 0
Python String Append

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:

  1. String Concatenation with +
  2. Using string.join() (Efficient for multiple appends)
  3. Using the += Operator (Extending string)
  4. 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.

String Concatenation with + in Python

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

  1. The + operator is flexible because it allows us to chain concatenations. e.g., s = a + b + c
  2. Ideal for quick, one-off appends.

Cons

  1. Each + creates a new string, leading to O(n²) time complexity for repeated appends.
  2. 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.

Using string join() to append multiple strings in PythonIn 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

  1. It appends to a list that is O(1) amortized, making loops O(n).
  2. It avoids intermediate strings, which can be memory-consuming.

Cons

  1. It requires us to convert to a list and manage that list.
  2. 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”.

Using the += Operator to append a string

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

  1. It is a simple and concise method.
  2. It clearly expresses its intention to add a string.

Cons

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

Using f-strings

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.

Using string.format() method

first_name = "Millie"
last_name = "Brown"

print("The appended string is: {} {}".format(first_name, last_name))

# Output: The appended string is: Millie Brown

Pros

  1. It seamlessly integrates variables and expressions.
  2. The .format() method supports positional and keyword arguments.
  3. The f-strings approach is highly intuitive for complex templates.

Cons

  1. The f-strings require Python ≥3.6.
  2. If no variables are involved, then it can be overkill.

That’s it.

Post Views: 17
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Python Find in List: How to Find the Index of an Element in List
How to Print Single and Multiple Variables in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend