DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk2 min

How to Append to a String in Python

Python strings cannot be changed in place. Use += for a short addition, f-strings for interpolation, and join() or StringIO to assemble many fragments efficiently.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python strings are immutable: you can’t add characters to an existing string object in place. To extend a string, create a new string with concatenation or formatting and assign it back. For a few additions, use +=; for many fragments, collect them and join once with str.join(), or write them to io.StringIO.

Append a small amount of text with +=

For one or a few known additions, concatenate the strings and assign the result back to the variable:

text = "Hello"
text += "!"
print(text)  # Hello!

The augmented assignment rebinds text to the concatenated result; it does not mutate the original string in place. You can also write text = text + extra.

Insert variables into a message with an f-string

When you are building a message from a template and variable values, an f-string is usually clearer than assembling each piece separately:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
DBI/SALA Python,1500121,Small Parts Pouch Makes It Nearly Impossible For Objects To Fall Out,No Opening/Closing Necessary,Canvas Orange
  • Designed with drop prevention in mind
  • Patented Python Safety Small Parts Pouch stops accidental drops the moment something is put inside
  • Innovative self-closure system traps objects inside, the Small Parts Pouch makes it nearly impossible for objects to fall out once placed in the bag
  • Easy for retrieval by the user since no opening or closing is necessary
name = "Ada"
message = f"Hello, {name}!"

The expressions inside the braces are evaluated to create a new string. Formatted string literals were added in Python 3.6.

Build a string from many fragments

If a loop or other process produces many pieces, store them in a list and combine them once with join():

parts = ["Hello", ", ", "world", "!"]
text = "".join(parts)
print(text)  # Hello, world!

The string before .join() is the separator placed between the parts. Use "".join(parts) to add no separator, or " ".join(parts) to put a space between each item.

For incremental construction where you write pieces as they become available, use io.StringIO:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Python No Spill Clean and Fill Aquarium Gravel Tube Hose Extension, 20-Feet
  • Provides an extra 20 feet of length
  • Easily attaches to any size Python No Spill Clean and Fill
  • Suitable for use in both freshwater and marine environments
  • Made of durable and petsafe vinyl tubing
  • Comes included with 1 female and 1 male adapter for easy connection
from io import StringIO

buffer = StringIO()
buffer.write("Hello")
buffer.write("!")
text = buffer.getvalue()

Why repeated concatenation can be inefficient

Python’s documentation explains that concatenating immutable sequences creates a new object. Repeatedly extending a string with concatenation can therefore take quadratic total runtime as the accumulated string grows. For many fragments, collecting pieces and calling join() once, or writing them to StringIO, is the documented efficient approach, with linear total runtime cost. This is a concern for repeated assembly—not a reason to avoid + or += for a couple of known pieces.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Add or replace characters at a particular position

Strings do not have an in-place insertion or replacement method. Use slicing to construct a new value. To insert extra at index i:

text = text[:i] + extra + text[i:]

To replace the character at index i with replacement:

text = text[:i] + replacement + text[i + 1:]

Why string.append() does not work

append() is a list method, not a string method. Since strings have no mutable append operation, calling string.append("x") raises an attribute error. Use concatenation and reassignment for a short addition, or collect fragments in a list and join them when you are building a string repeatedly.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick Recap

Bestseller No. 1
DBI/SALA Python,1500121,Small Parts Pouch Makes It Nearly Impossible For Objects To Fall Out,No Opening/Closing Necessary,Canvas Orange
DBI/SALA Python,1500121,Small Parts Pouch Makes It Nearly Impossible For Objects To Fall Out,No Opening/Closing Necessary,Canvas Orange
Designed with drop prevention in mind; Easy for retrieval by the user since no opening or closing is necessary
$92.14
Bestseller No. 2
Python No Spill Clean and Fill Aquarium Gravel Tube Hose Extension, 20-Feet
Python No Spill Clean and Fill Aquarium Gravel Tube Hose Extension, 20-Feet
Provides an extra 20 feet of length; Easily attaches to any size Python No Spill Clean and Fill
$42.98

Choose the right string-building method

Situation Approach
A few known additions text += extra or text = text + extra
A message with variable values An f-string, such as f"Hello, {name}!"
Many fragments already collected separator.join(parts)
Pieces written incrementally io.StringIO

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.