Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Python | Add substring at specific index

  • Last Updated : 07 Jun, 2019

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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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))
Output :
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))
Output :
The original string : geeksgeeks
The add string : for
The string after performing addition : geeksforgeeks



My Personal Notes arrow_drop_up
Recommended Articles
Page :