Insert characters into a string at an index in Python

avatar

Borislav Hadzhiev

Last updated: Sep 21, 2022

banner

Photo from Unsplash

Insert characters into a string at an index in Python #

To insert characters into a string at an index:

  1. Use string slicing to split the string at the specified index.
  2. Use the addition (+) operator to insert the characters between the two slices.
  3. The new string will have the specified characters inserted at the given index.
main.py
my_str = 'bobby.com' my_str = my_str[:5] + 'hadz' + my_str[5:] print(my_str) # 👉️ bobbyhadz.com

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

The slice my_str[:5] goes up to, but not including the character at index 5.

The slice my_str[5:] starts at the character at index 5 and goes to the end of the string.

If you need to get the index of a character in the string, use the str.index() method.

main.py
my_str = 'bobby.com' index = my_str.index('.') print(index) # 👉️ 5 my_str = my_str[:index] + 'hadz' + my_str[index:] print(my_str) # 👉️ bobbyhadz.com

The str.index method returns the index of the first occurrence of the provided substring in the string.

The method raises a ValueError if the substring is not found in the string.

Python strings are immutable, so we reassigned the variable that stores the original string.

If you want to keep the original string around, declare a new variable.

main.py
my_str = 'bobby.com' new_str = my_str[:5] + 'hadz' + my_str[5:] print(new_str) # 👉️ bobbyhadz.com

There is also a str.rindex() method that returns the highest index in the string where the provided substring is found.

main.py
my_str = 'one.two.four' index = my_str.rindex('.') print(index) # 👉️ 7 my_str = my_str[:index] + '.three' + my_str[index:] print(my_str) # 👉️ one.two.three.four

The rindex() method returned the rightmost index of a period in the string and we used the index to insert a substring into the string.

We added the characters right before the second period in the string.

Alternatively, you can use the list() class.

Insert characters into a string at an index using list() #

To insert characters into a string at an index:

  1. Use the list() class to convert the string to a list of characters.
  2. Use the list.insert() method to insert characters at the given index.
  3. Use the str.join() method to join the list into a string.
main.py
my_str = 'bobby.com' my_list = list(my_str) print(my_list) # 👉️ ['b', 'o', 'b', 'b', 'y', '.', 'c', 'o', 'm'] my_list.insert(5, 'hadz') new_str = ''.join(my_list) print(new_str) # 👉️ bobbyhadz.com

We used the list() class to convert the string to a list of characters.

main.py
my_str = 'bobby.com' my_list = list(my_str) # 👇️ ['b', 'o', 'b', 'b', 'y', '.', 'c', 'o', 'm'] print(my_list)

The next step is to use the list.insert() method to insert the string into the list.

main.py
my_str = 'bobby.com' my_list = list(my_str) my_list.insert(5, 'hadz') # 👇️ ['b', 'o', 'b', 'b', 'y', 'hadz', '.', 'c', 'o', 'm'] print(my_list)

The list.insert method inserts an item at a given position.

The method takes the following 2 parameters:

NameDescription
indexThe index of the element before which to insert
itemThe item to be inserted at the given index

The last step is to use the str.join() method to join the list into a string.

main.py
my_str = 'bobby.com' my_list = list(my_str) my_list.insert(5, 'hadz') new_str = ''.join(my_list) print(new_str) # 👉️ bobbyhadz.com

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

We used an empty string separator to join the elements of the list without a delimiter.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.