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 a string.join() (Efficient for multiple appends)
  3. Using the += Operator (Extending a 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

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

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 so like s += “new part”.

Using the += Operator to append a string

In the above illustration, we first defined a name and, using the += operator, appended new names to the original name. The final string is the appended string.

name = "Millie"
name += " Bobby"
name += " Brown"

print(name)  # Millie Bobby Brown

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 placeholders to insert variables, 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

That’s it.

Post Views: 46
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