Python – Replace to K at ith Index in String
Given a String, replace ith index by K value.
Input : test_str = 'geeks5geeks', K = '7', i = 5
Output : 'geeks7geeks'
Explanation : Element is 5, converted to 7 on ith index.
Input : test_str = 'geeks5geeks', K = '7', i = 6
Output : 'geeks56eeks'
Explanation : Element is g, converted to 7 on ith index.
Method #1: Using string slicing
In this, we perform the slicing of pre string, till i, and then add K, then add post values, using string slice method.
Python3
test_str = 'geeks5geeks'
print ( "The original string is : " + str (test_str))
K = '4'
i = 5
res = test_str[: i] + K + test_str[i + 1 :]
print ( "The constructed string : " + str (res))
|
Output
The original string is : geeks5geeks
The constructed string : geeks4geeks
Time Complexity: O(n) -> string slicing
Auxiliary Space: O(n)
Method #2 : Using join() + generator expression
In this, we perform the task of checking for ith index and conditionally appending K, using generator expression and convert the result into string using join().
Python3
test_str = 'geeks5geeks'
print ( "The original string is : " + str (test_str))
K = '4'
i = 5
res = ''.join(test_str[idx] if idx ! = i else K for idx in range ( len (test_str)))
print ( "The constructed string : " + str (res))
|
Output
The original string is : geeks5geeks
The constructed string : geeks4geeks
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using replace() method
Python3
test_str = 'geeks5geeks'
print ( "The original string is : " + str (test_str))
K = '4'
i = 5
test_str = test_str.replace(test_str[i],K, 1 )
print ( "The constructed string : " + str (test_str))
|
Output
The original string is : geeks5geeks
The constructed string : geeks4geeks
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using list() and join() methods
Python3
test_str = 'geeks5geeks'
print ( "The original string is : " + str (test_str))
K = '4'
i = 5
x = list (test_str)
x[i] = K
res = "".join(x)
print ( "The constructed string : " + str (res))
|
Output
The original string is : geeks5geeks
The constructed string : geeks4geeks
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using bytearray()
- Initialize the string test_str and print it.
- Initialize the values of K and i.
- Convert the string to a bytearray using bytearray(test_str, encoding=’utf-8′). This creates a mutable bytearray object that can be modified in-place.
- Replace the character at index i with K by assigning K to test_str[i].
- Convert the bytearray back to a string using bytes.decode() method.
- Print the resulting string.
Python3
test_str = 'geeks5geeks'
print ( "The original string is : " + str (test_str))
K = '4'
i = 5
byte_str = bytearray(test_str, encoding = 'utf-8' )
byte_str[i] = ord (K)
res = byte_str.decode()
print ( "The constructed string : " + str (res))
|
Output
The original string is : geeks5geeks
The constructed string : geeks4geeks
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string.
Method #6: Using string concatenation and string indexing
Step-by-step approach:
- Initialize the original string, K, and i variables.
- Create a new string variable to store the modified string.
- Loop through each character in the original string, adding each character to the new string until you reach the index i.
- Add the character K to the new string.
- Continue looping through the original string, starting at the index i+1, and adding each character to the new string.
- Print the new string.
Below is the implementation of the above approach:
Python3
test_str = 'geeks5geeks'
print ( "The original string is : " + str (test_str))
K = '4'
i = 5
new_str = ''
for j in range ( len (test_str)):
if j = = i:
new_str + = K
else :
new_str + = test_str[j]
print ( "The constructed string : " + str (new_str))
|
Output
The original string is : geeks5geeks
The constructed string : geeks4geeks
Time complexity: O(n), where n is the length of the original string.
Auxiliary space: O(n), where n is the length of the original string, since we are creating a new string to store the modified string.
Last Updated :
05 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...