Python | Range duplication in String

Sometimes, while working with Strings, we can have a problem in which we need to replicate the range of strings consecutively in range. This kind of problem can have application in day-day programming domain. Lets discuss certain way in which this task can be performed.

Method : Using string slicing
This is straight forward way to solve this problem. In this, we perform the task of slicing the string to be repeated and attach its duplicate. The pre and post slices are attached to its prefix and suffic respectively to construct result string.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Range duplication in String
# Using string slicing
  
# initializing string
test_str = "geeksforgeeks"
  
# printing original string
print("The original string is : " + test_str)
  
# initializing range 
i, j = 3, 6
  
# Range duplication in String
# Using string slicing
temp = test_str[i:j] * 2 
res = test_str[:i] + temp + test_str[j:]
  
# printing result 
print("The string after range duplication : " + res) 

chevron_right


Output :

The original string is : geeksforgeeks
The string after range duplication : geeksfksforgeeks
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.