October 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 ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Insert a Character Into a String in Python at Any Index

Insert text at any valid position with Python slicing: combine the part before the index, the inserted text, and the part after it.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use slicing to build a new string with the character at the desired position: result = text[:index] + character + text[index:]. Python strings are immutable, so this creates a new string rather than changing the original.

Insert a character with slicing

Split the original string at the insertion boundary, put the new text between the two slices, and concatenate them:

text = "hello"
index = 2
character = "X"

result = text[:index] + character + text[index:]
print(result)  # heXllo

The same expression inserts a substring, not just one character. Python has no separate character type: a string element at one index is itself a string of length one. See the Python documentation on built-in types.

Choose the insertion index

Python string positions start at 0, while a slice boundary marks the point between characters. The starting boundary is included in the left slice and excluded from the right slice, so text[:i] and text[i:] divide the string at boundary i. The Python tutorial describes this slicing behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • index = 0 inserts before the first character.
  • index = len(text) inserts after the last character, effectively appending.
  • For a string of length n, valid insertion boundaries run from 0 through n, inclusive.
text = "hello"

at_start = "X" + text
at_end = text + "X"

Reject out-of-range positions when needed

Slice bounds are clipped to the available string. That can make an invalid index look like a valid insertion at the beginning or end. If your code should reject positions outside the valid boundaries, check the index before slicing:

if not 0 <= index <= len(text):
    raise IndexError("insertion index out of range")

result = text[:index] + character + text[index:]

Why str.insert() does not work

Strings are immutable sequences, so Python does not provide an in-place insert() method for str. Slicing and concatenation make a new string. An f-string also constructs a new string; it does not modify an existing one. See the Python documentation for strings and sequence types and formatted string literals.

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

Use a list for repeated positional edits

If you need mutable sequence operations, convert the string to a list, call list.insert(), then join the elements back into a string:

text = "hello"
characters = list(text)
characters.insert(2, "X")
result = "".join(characters)
print(result)  # heXllo

This pattern is useful when the surrounding work needs a mutable sequence. If you only need one insertion, the slice expression is simpler. For assembling many fragments, Python documents str.join() and io.StringIO as construction options; which is more suitable depends on whether fragments are already collected or written incrementally. The documentation does not establish a universal performance crossover point.

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

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
Outdated Drivers Are Slowing You DownFree scan - exact matches
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.