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.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →#1 Best Overall
index = 0inserts before the first character.index = len(text)inserts after the last character, effectively appending.- For a string of length
n, valid insertion boundaries run from0throughn, 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.
Rank #2
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.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →Quick Recap
Best Value
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.




