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 characters 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(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
Note: This solution is much slower (has O(n^2) time complexity) than the other methods. If speed is needed, method #3 is similar in logic and is faster (it has O(n) time complexity).
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 a 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 the 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
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.
Recommended Posts:
- Python | Ways to split a string in different ways
- Python | Ways to remove numeric digits from given string
- Python | Ways to remove n characters from start of given string
- Python - Ways to remove multiple empty spaces from string List
- Python - Remove Initial character in String List
- Python | Remove List elements containing given String character
- Ways to increment a character in Python
- Python | Ways to remove a key from dictionary
- Python | Ways to remove particular list element
- Python - Ways to remove duplicates from list
- Python | Insert character after every character pair
- Python program to read character by character from a file
- Python - Create a Dictionary with Key as First Character and Value as Words Starting with that Character
- Python | Remove last character in list of strings
- Python | Remove tuple from list of tuples if not containing any character
- Python | Remove Kth character from strings list
- Python | Remove given character from Strings list
- Python - Remove given character from first element of Tuple
- Python - Remove strings with any non-required character
- Python - Remove front K characters from each string in String List
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.
Improved By : stulikesmail

