Open In App

Python | Deleting all occurrences of character

Last Updated : 12 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

These days string manipulation is very popular in Python, and due to its immutable character, sometimes, it becomes more important to know its working and hacks. This particular article solves the problem of deleting all occurrences of a character from a string. Let’s discuss ways in which this can be achieved. 

Method #1: Using translate() Usually this function is used to convert a particular character to some other character. By translating the resultant removal character to “None”, this function can perform this task. This function works only for Python2 

Python




# Python code to demonstrate working of
# Deleting all occurrences of character
# Using translate()
 
# initializing string
test_str = "GeeksforGeeks"
 
# initializing removal character
rem_char = "e"
 
# printing original string
print(" The original string is: "
      + str(test_str))
 
# Using translate()
# Deleting all occurrences of character
res = test_str.translate(None, rem_char)
 
# printing result
print(" quot The string after character deletion: "
      + str(res))


Output : 

The original string is : GeeksforGeeks
The string after character deletion : GksforGks

Time Complexity: O(n)
Auxiliary Space : O(n)

Method #2: Using replace() This function works functionally quite similar to the above function, but it is recommended due to several reasons. It can be used in newer versions of Python and is more efficient than the above function. We replace the empty string instead of None as above for using this function to perform this task. 

Python3




# Python3 code to demonstrate working of
# Deleting all occurrences of character
# Using replace()
 
# initializing string
test_str = "GeeksforGeeks"
 
# initializing removal character
rem_char = "e"
 
# printing original string
print("The original string is :" + str(test_str))
 
# Using replace()
# Deleting all occurrences of character
res = test_str.replace(rem_char, "")
 
# printing result
print("The string after character deletion : " + str(res))


Output : 

The original string is : GeeksforGeeks
The string after character deletion : GksforGks

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3: Without using any built-in methods

Python3




# Python3 code to demonstrate working of
# Deleting all occurrences of character
 
 
# initializing string
test_str = "GeeksforGeeks"
 
# initializing removal character
rem_char = "e"
 
# printing original string
print("The original string is : " + str(test_str))
 
new_str = ""
# Deleting all occurrences of character
for i in test_str:
    if(i != rem_char):
        new_str += i
res = new_str
# printing result
print("The string after character deletion : " + str(res))


Output

The original string is : GeeksforGeeks
The string after character deletion : GksforGks

Time Complexity: O(n)
Auxiliary Space : O(n)

Method #4: Using List comprehension

List comprehension is a concise way of creating lists in Python. It allows you to create a new list by applying a certain operation to each element of an existing iterable (e.g. a list, tuple, set, etc.). In this case, in Method #4, we use list comprehension to delete all occurrences of a specific character from a string.

Python3




# Python3 code to demonstrate working of
# Deleting all occurrences of character
 
# initializing string
test_str = "GeeksforGeeks"
 
# initializing removal character
rem_char = "e"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Using List comprehension
res = "".join([i for i in test_str if i != rem_char])
 
# printing result
print("The string after character deletion : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original string is : GeeksforGeeks
The string after character deletion : GksforGks

Time Complexity: O(n)
Auxiliary Space: O(n)

METHOD 5:Using re module.

APPROACH:

In this Approach , we define the input string and the target character as variables. Then, we call the delete_all_occurrences_5() function with the input string and target character as arguments. The function uses the re.sub() method to delete all occurrences of the target character from the string. Finally, we print the original and modified strings using print()

ALGORITHM:

1.Call re.sub(char, ”, string) to delete all occurrences of the target character from the input string.
2.Assign the modified string to the output_string variable.
3.Return the output_string.

Python3




import re
 
def delete_all_occurrences_5(string, char):
    return re.sub(char, '', string)
 
# Example usage
input_string = 'GeeksforGeeks'
target_char = 'e'
 
# Call the function to delete all occurrences of the target character
output_string = delete_all_occurrences_5(input_string, target_char)
 
# Print the output
print('The original string is :', input_string)
print('The string after character deletion :', output_string)


Output

The original string is : GeeksforGeeks
The string after character deletion : GksforGks

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads