Python – Add Phrase in middle of String
Given a String, add a phrase in the middle of it.
Input : test_str = ‘geekforgeeks is for geeks’, mid_str = “good”
Output : geekforgeeks is good for geeks
Explanation : Added just in middle, after 2 words.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
Input : test_str = ‘geekforgeeks best’, mid_str = “is”
Output : geekforgeeks is best
Explanation : Added just in middle, after 1 word.
Method #1 : Using split() + slicing + join()
In this, Strings are converted to a list of words, then the middle position is extracted to append a new phrase. After addition, the string is back converted using join().
Python3
# Python3 code to demonstrate working of# Add Phrase in middle of String# Using split() + slicing + join() # initializing stringtest_str = 'geekforgeeks is for geeks' # printing original stringprint("The original string is : " + str(test_str)) # initializing mid stringmid_str = "best" # splitting string to listtemp = test_str.split()mid_pos = len(temp) // 2 # appending in midres = temp[:mid_pos] + [mid_str] + temp[mid_pos:] # conversion backres = ' '.join(res) # printing resultprint("Formulated String : " + str(res)) |
The original string is : geekforgeeks is for geeks Formulated String : geekforgeeks is best for geeks
Method #2 : Using split() + slicing + join() [ more compact]
Similar to the above method, just one-liner way to solve this problem, for more compact.
Python3
# Python3 code to demonstrate working of# Add Phrase in middle of String# Using split() + slicing + join() # initializing stringtest_str = 'geekforgeeks is for geeks' # printing original stringprint("The original string is : " + str(test_str)) # initializing mid stringmid_str = "best" # splitting string to listtemp = test_str.split()mid_pos = len(temp) // 2 # joining and construction using single lineres = ' '.join(temp[:mid_pos] + [mid_str] + temp[mid_pos:]) # printing resultprint("Formulated String : " + str(res)) |
The original string is : geekforgeeks is for geeks Formulated String : geekforgeeks is best for geeks


