Saturday, November 16, 2024
Google search engine
HomeLanguagesPython program for removing i-th character from a string

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 Lazyroar then its indexing will be as –

g e e k s
0 1 2 3 4

Examples :

Input : Geek
        i = 1
Output : Gek

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 : 

Python




# Python3 program for removing i-th
# indexed character from a string
  
# Removes character at index i
  
  
def remove(string, i):
  
    # Characters before the i-th indexed
    # is stored in a variable a
    a = string[: i]
  
    # Characters after the nth indexed
    # is stored in a variable b
    b = string[i + 1:]
  
    # Returning string after removing
    # nth indexed character.
    return a + b
  
  
# Driver Code
if __name__ == '__main__':
  
    string = "LazyroarFORLazyroar"
  
    # Remove nth index element
    i = 5
  
    # Print the new string
    print(remove(string, i))


Output

LazyroarORLazyroar

Time Complexity: O(n) where n is the length of the string.
Auxiliary Space: O(n) where n is the length of the string.

Approach 2 : The idea is to use string replace in Python 

Python




# Python3 program for removing i-th
# indexed character from a string
  
# Removes character at index i
  
  
def remove(string, i):
  
    for j in range(len(string)):
        if j == i:
            string = string.replace(string[i], "", 1)
    return string
  
  
# Driver Code
if __name__ == '__main__':
  
    string = "LazyroarFORLazyroar"
  
    # Remove nth index element
    i = 5
  
    # Print the new string
    print(remove(string, i))


Output

LazyroarORLazyroar

Time Complexity: O(n) where n is the length of the string. This is because the function performs a single loop through the string and calls the replace function once.
Auxiliary Space: O(1) because the function uses only a few variables and no additional data structures are created.

Approach 3 : Using list(),pop() and join() methods

Python3




# Python3 program for removing i-th
# indexed character from a string
  
# Removes character at index i
  
  
def remove(string, i):
    if i > len(string):
        return string
    a = list(string)
    a.pop(i)
    return "".join(a)
  
  
# Driver Code
if __name__ == '__main__':
  
    string = "LazyroarFORLazyroar"
  
    # Remove nth index element
    i = 2
  
    # Print the new string
    print(remove(string, i))


Output

geksFORLazyroar

Time Complexity: O(n) where n is the length of the string

Auxiliary Space: O(n) where n is the length of the string 

Approach 4: Using enumerate, join

Python3




def remove(string, i):
  return "".join()
  
print(remove("neveropen",2))


Output

geksforLazyroar

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.

Time complexity: 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.
Auxiliary Space: O(n) because it involves creating a new string with the same length as the input string.

Approach 5: Using the re module and f-string 

  • Import the re module
  • pattern = f”(^.{{{i}}})(.)” Create a regular expression pattern that matches the character along with the substring before it. ^ matches the start of the string, {i} specifies the index of the character to be removed,
  • re.sub(pattern, r”\1″, string) is used to substitute the matched pattern with the captured substring before the given index.

Python3




# Python3 program for removing i-th
# indexed character from a string
  
# Removes character at index i
  
import re
  
def remove(string, i):
    pattern = f"(^.{{{i}}})(.)"
    return re.sub(pattern, r"\1", string)
  
# Driver Code
if __name__ == '__main__':
    string = "LazyroarFORLazyroar"
    #i-th index to be removed
    i = 5
    #string after removal of i-th index
    print(remove(string, i))


Output

LazyroarORLazyroar

Time Complexity: O(N)  where n is the length of the input string as it iterates over all characters in the string to find the matching pattern.

Space Complexity: O(N) where n is the length of the input string as we creates a new string with the same length

RELATED ARTICLES

Most Popular

Recent Comments