Python – Insert character at specific index in string

Insert character at specific index in String

To insert a character at specific index of string in Python, we can split the string at specific index, and then join the first part of the string, the character, and the last part of the string.

The syntax to insert character ch at the index i of string x is

x[:i] + ch + x[i:]

Example

In the following program, we take a string x, and insert the character ch at index i, and prepend the character to the starting of the string.

Python Program

x = 'apple'
ch = 'm'
i = 3

output = x[:i] + ch + x[i:]

print(output)
Run Code Copy

Output

appmle

Summary

In this tutorial of Python Examples, we learned how to insert a character at specific index in the string using String Slicing and String Concatenation Operator, with the help of well detailed examples.

Related Tutorials

Code copied to clipboard successfully 👍