Open In App

Python – Replace to K at ith Index in String

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Replace to K at ith Index in String
# using string slicing
 
# initializing strings
test_str = 'geeks5geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = '4'
 
# initializing i
i = 5
 
# the replaced result
res = test_str[: i] + K + test_str[i + 1:]
 
# printing result
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




# Python3 code to demonstrate working of
# Replace to K at ith Index in String
# using join() + generator expression
 
# initializing strings
test_str = 'geeks5geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = '4'
 
# initializing i
i = 5
 
# the replaced result
res = ''.join(test_str[idx] if idx != i else K for idx in range(len(test_str)))
 
# printing result
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




# Python3 code to demonstrate working of
# Replace to K at ith Index in String
# using string slicing
 
# initializing strings
test_str = 'geeks5geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = '4'
 
# initializing i
i = 5
 
test_str=test_str.replace(test_str[i],K,1)
# printing result
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




# Python3 code to demonstrate working of
# Replace to K at ith Index in String
 
# Initializing strings
test_str = 'geeks5geeks'
 
# Printing original string
print("The original string is : " + str(test_str))
 
# Initializing K
K = '4'
 
# Initializing i
i = 5
 
x = list(test_str)
x[i] = K
res = "".join(x)
 
# Printing result
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()

  1. Initialize the string test_str and print it.
  2. Initialize the values of K and i.
  3. 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.
  4. Replace the character at index i with K by assigning K to test_str[i].
  5. Convert the bytearray back to a string using bytes.decode() method.
  6. Print the resulting string.

Python3




# Python3 code to demonstrate working of
# Replace to K at ith Index in String
 
# Initializing strings
test_str = 'geeks5geeks'
 
# Printing original string
print("The original string is : " + str(test_str))
 
# Initializing K
K = '4'
 
# Initializing i
i = 5
 
# Convert string to bytearray
byte_str = bytearray(test_str, encoding='utf-8')
 
# Replace character at index i with K
byte_str[i] = ord(K)
 
# Convert bytearray back to string
res = byte_str.decode()
 
# Printing result
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




# Initializing strings
test_str = 'geeks5geeks'
 
# Printing original string
print("The original string is : " + str(test_str))
 
# Initializing K
K = '4'
 
# Initializing i
i = 5
 
# Create a new string variable to store the modified string
new_str = ''
 
# Loop through each character in the original string
for j in range(len(test_str)):
     
    # If we have reached the index i, add the character K to the new string
    if j == i:
        new_str += K
     
    # Otherwise, add the current character to the new string
    else:
        new_str += test_str[j]
 
# Printing result
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
Previous
Next
Share your thoughts in the comments
Similar Reads