Ways to remove i’th character from string in Python

In Python it is all known that string is immutable and hence sometimes poses visible restrictions while coding the constructs that are required in day-day programming. This article presents one such problem of removing i’th character from string and talks about possible solutions that can be employed in achieving them.

Method 1 : Naive Method

In this method, one just has to run a loop and append the character as they come and build a new string from the existing one except when the index is i.

Code #1 : Demonstrating Naive method to remove i’th char from string.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# method to remove i'th character
# Naive Method
  
# Initializing String 
test_str = "GeeksForGeeks"
  
# Printing original string 
print ("The original string is : " + test_str)
  
# Removing char at pos 3
# using loop
new_str = ""
  
for i in range(0, len(test_str)):
    if i != 2:
        new_str = new_str + test_str[i]
  
# Printing string after removal  
print ("The string after removal of i'th character : " + new_str)

chevron_right


Output:

The original string is : GeeksForGeeks
The string after removal of i'th character : GeksForGeeks

Method 2 : Using str.replace()

replace() can possibly be used for performing the task of removal as we can replace the particular index with empty char, and hence solve the issue.

Drawback : The major drawback of this approach is that it fails in case there as duplicates in string that match the char at pos. i. replace() replaces all the occurrences of a particular character and hence would replace all the occurrences of all the characters at pos i. We can still sometimes use this function if the replacing character occurs 1st time in the string.

Code #2 : Demonstrating the use of str.replace() to remove i’th char.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# method to remove i'th character
# using replace()
  
# Initializing String 
test_str = "GeeksForGeeks"
  
# Printing original string 
print ("The original string is : " + test_str)
  
# Removing char at pos 3
# using replace
new_str = test_str.replace('e', '')
  
# Printing string after removal  
# removes all occurrences of 'e'
print ("The string after removal of i'th character( doesn't work) : " + new_str)
  
# Removing 1st occurrence of s, i.e 5th pos.
# if we wish to remove it.
new_str = test_str.replace('s', '', 1)
  
# Printing string after removal  
# removes first occurrences of s
print ("The string after removal of i'th character(works) : " + new_str)

chevron_right


Output:

The original string is : GeeksForGeeks
The string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks

Method 3 : Using slice + concatenation

Once can use string slice and slice the string before the pos i, and slice after the pos i. Then using string concatenation of both, i’th character can appear to be deleted from string.

Code #3 : Demonstrating the use of slice and concatenation to remove the i’th char.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# method to remove i'th character
# using slice + concatenation
  
# Initializing String 
test_str = "GeeksForGeeks"
  
# Printing original string 
print ("The original string is : " + test_str)
  
# Removing char at pos 3
# using slice + concatenation
new_str = test_str[:2] +  test_str[3:]
  
# Printing string after removal  
# removes ele. at 3rd index
print ("The string after removal of i'th character : " + new_str)

chevron_right


Output :

The original string is : GeeksForGeeks
The string after removal of i'th character : GeksForGeeks

Method 4 : Using str.join() and list comprehension

In this method, each element of string is first converted as each element of list, and then each of them is joined to form a string except the specified index.

Code #4 : Demonstrating str.join() and list comprehension to remove i’th index char.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# method to remove i'th character
# using join() + list comprehension
  
# Initializing String 
test_str = "GeeksForGeeks"
  
# Printing original string 
print ("The original string is : " + test_str)
  
# Removing char at pos 3
# using join() + list comprehension
new_str = ''.join([test_str[i] for i in range(len(test_str)) if i != 2])
  
# Printing string after removal  
# removes ele. at 3rd index
print ("The string after removal of i'th character : " + new_str)

chevron_right


Output :

The original string is : GeeksForGeeks
The string after removal of i'th character : GeksForGeeks


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.