Monday, November 18, 2024
Google search engine
HomeLanguagesAdd Substring at Specific Index Python

Add Substring at Specific Index Python

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. 

Add substring 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




# Python3 code to demonstrate
# Add substring at specific index
# using list slicing
 
# initializing string
test_string = 'LazyroarLazyroar'
 
# 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 : LazyroarLazyroar
The add string : for
The string after performing addition : neveropen

Time complexity: O(1)
Auxiliary space: O(1)

Add substring 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




# Python3 code to demonstrate
# Add substring at specific index
# using join() + list() + insert()
 
# initializing string
test_string = 'LazyroarLazyroar'
 
# 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 : LazyroarLazyroar
The add string : for
The string after performing addition : neveropen

Add substring use string concatenation

Here are the steps:

Initialize the original string and the substring to add.
Determine the index where to add the substring.
Create a new string by concatenating the part of the original string before the index, the substring to add, and the part of the original string after the index.
Print the result.

Python3




# Python3 code to demonstrate
# Add substring at specific index
# using string concatenation
 
# initializing string
test_string = 'LazyroarLazyroar'
 
# 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 string concatenation
# 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 : LazyroarLazyroar
The add string : for
The string after performing addition : neveropen

The time complexity of this approach is O(n), where n is the length of the original string. 
The auxiliary space used is O(n) as well because a new string is created to store the resul

Add substring Using f-string

APPROACH:

The given program adds a substring at a specific index in the input string using f-string in Python.

ALGORITHM:

1. Define the original string, substring to add, and the index to add the substring at.
2. Use string slicing to extract the original string before the index and after the index.
3. Concatenate the extracted substrings with the substring to add in the middle.
4. Store the result in a variable.
5. Print the result.

Python3




# Input string
original_str = 'LazyroarLazyroar'
# Substring to add
add_str = 'for'
# Index to add substring
index = 5
 
# Adding substring
result_str = f"{original_str[:index]}{add_str}{original_str[index:]}"
 
# Printing the result
print(result_str)


Output

neveropen

Time complexity:
The time complexity of the program is O(n), where n is the length of the original string. This is because the program needs to slice the original string and concatenate the substrings, both of which take linear time.

Space complexity:
The space complexity of the program is O(n), where n is the length of the original string. This is because the program creates a new string variable to store the result, which takes the same amount of space as the original string.

RELATED ARTICLES

Most Popular

Recent Comments