Python program for removing i-th character from a string
Given the string, we have to remove the ith indexed character from the string. In any string, indexing always start from 0. Suppose we have a string geeks then its indexing will be as –
g e e k s 0 1 2 3 4
Input : Geek i = 1 Output : Geek Input : Peter i = 4 Output : Pete
Approach 1 : From the given string, i-th indexed element has to be removed. So, Split the string into two halves, before indexed character and after indexed character. Return the merged string. Below is the implementation of above approach :
Approach 2 : The idea is to use string replace in Python
Approach 3 : Using list(),pop() and join() methods
Approach 4: Using enumerate, join
The above approach uses a list comprehension to create a new list of characters from the original string, but excluding the i-th character. It iterates over the original string and for each character, it checks if the index of the character (j) is not equal to the specified index (i). If it is not equal, the character is included in the new list. Then, the join method is used to combine the characters of the new list into a single string. This approach is more concise than the previous ones, as it combines the steps of creating a new list and joining it into a single line of code.
The time complexity of the function is O(n), where n is the length of the input string. This is because the function involves iterating through all the characters in the string and creating a new string by concatenating the characters that are not being removed.
The Auxiliary Space of the function is also O(n) because it involves creating a new string with the same length as the input string.