Python | Add substring at specific index
In Python, String is immutable datatype, what this means is, that there are lot many restrictions when one handles its manipulation. The problem of adding something at a position at string is not possible, without the list reconstruction. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list slicing
This task can be performed using the list slicing. In this, we just slice the list into two parts, breaking at the target position and then rejoining it after inserting target substring at middle.
# Python3 code to demonstrate # Add substring at specific index # using list slicing # initializing string test_string = 'geeksgeeks' # initializing add_string add_string = "for" # printing original string print("The original string : " + test_string) # printing add string print("The add string : " + add_string) # initializing N N = 5 # using list slicing # Add substring at specific index res = test_string[ : N] + add_string + test_string[N : ] # print result print("The string after performing addition : " + str(res)) |
The original string : geeksgeeks The add string : for The string after performing addition : geeksforgeeks
Method #2 : Using join() + list() + insert()
Another possible hack that can be performed in for the following problem is that converting the string to list and then adding the string at particular position and then performing the join.
# Python3 code to demonstrate # Add substring at specific index # using join() + list() + insert() # initializing string test_string = 'geeksgeeks' # initializing add_string add_string = "for" # printing original string print("The original string : " + test_string) # printing add string print("The add string : " + add_string) # initializing N N = 5 # using join() + list() + insert() # Add substring at specific index res = list(test_string) res.insert(N, add_string) res = ''.join(res) # print result print("The string after performing addition : " + str(res)) |
The original string : geeksgeeks The add string : for The string after performing addition : geeksforgeeks
Recommended Posts:
- Python | Index specific cyclic iteration in list
- Python | Get the starting index for all occurrences of given substring
- Python | Prefix extraction before specific character
- Python | Retain records of specific length
- Python | Extract specific keys from dictionary
- Detection of a specific color(blue here) using OpenCV with Python
- Python | Sort list of tuples by specific ordering
- Python | Remove element from given list containing specific digits
- Python | Find the list elements starting with specific letter
- Python | Index of Non-Zero elements in Python list
- Python | Frequency of substring in given string
- Python | Substring Key match in dictionary
- Python | All occurrences of substring in string
- Python | Get the string after occurrence of given substring
- Python | Find last occurrence of substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



