Skip to content
Related Articles

Related Articles

Python – Replace to K at ith Index in String

View Discussion
Improve Article
Save Article
  • Last Updated : 30 Aug, 2022
View Discussion
Improve Article
Save Article

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)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!