Skip to content
Related Articles

Related Articles

Ways to remove i’th character from string in Python

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 13 Sep, 2022
Improve Article
Save Article

This article presents one such problem of removing the ith character from a string and talks about possible solutions that can be employed in achieving them using Python.

Remove the ith character from the string using the native method

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

Python3




test_str = "GeeksForGeeks"
 
# Removing char at pos 3
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 string after removal of i'th character : GeksForGeeks

Remove the ith character from the string using str.replace()

The str.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 for 1st time in the string. 

Python3




# Initializing String
test_str = "GeeksForGeeks"
 
# 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 string after removal of i'th character( doesn't work) : GksForGks
The string after removal of i'th character(works) : GeekForGeeks

Remove the ith character from the string using slice + concatenation

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

Python3




# Initializing String
test_str = "GeeksForGeeks"
 
# 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 string after removal of i'th character : GeksForGeeks

Remove the ith character from the string using str.join() and list comprehension

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

Python3




# Initializing String
test_str = "GeeksForGeeks"
 
# 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 string after removal of i'th character : GeksForGeeks

Remove the specific character from the string using translate()

In this method, we removed 123 from Geeksforgeeks using string.translate()

Python3




str = 'Geeks123For123Geeks'
  
print(str.translate({ord(i): None for i in '123'}))

Output:

GeeksForGeeks

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!