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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


Output

The original string is : geeks5geeks
The constructed string : geeks4geeks

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


Output

The original string is : geeks5geeks
The constructed string : geeks4geeks

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




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.