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.
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.
# 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) |
Output:
The original string is : GeeksForGeeks The string after removal of i'th character : GeksForGeeks
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.
# 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) |
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
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.
# 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) |
Output :
The original string is : GeeksForGeeks The string after removal of i'th character : GeksForGeeks
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.
# 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) |
Output :
The original string is : GeeksForGeeks The string after removal of i'th character : GeksForGeeks
Recommended Posts:
- Python | Ways to remove numeric digits from given string
- Python | Ways to remove n characters from start of given string
- Ways to increment a character in Python
- Python | Ways to remove a key from dictionary
- Python | Ways to remove duplicates from list
- Python | Ways to remove particular list element
- Python | Remove last character in list of strings
- Python | Remove tuple from list of tuples if not containing any character
- Reverse string in Python (5 different ways)
- Python | Frequency of each character in String
- Python | Lowercase first character of String
- Python | Ways to check string contain all same characters
- Python | Ways to concatenate boolean to string
- Python | Ways to check if given string contains only letter
- Python | Ways to find all permutation of a string
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.



