Remove a given character from Strings list using Python


Introduction

In Python, you frequently encounter scenarios where you must eliminate a particular character from a string list. When you want to remove specific characters or symbols from your data, this becomes useful. This article will investigate two approaches for removing a specific character from a Python list of strings.

Removing a given character from Strings list using Python

Definition

Python can be used to remove a given character from a list of strings. The Python programming language involves removing a specific character from a list of strings during the process. Various methods can accomplish this common task in data cleaning and preprocessing. The objective is to eliminate the specified character by modifying the strings within the list. The list's original order and structure are maintained.

Algorithm

  • Step 1: Declare a string variable.

  • Step 2: Use the appropriate method to remove the given character from the string.

  • Step 3: Print the modified string.

  • Step 4: Repeat steps 1-3 for different characters to be removed.

  • Step 5: End.

Approach

  • Approach 1− Using replace () method

  • Approach 2− Using translate () method

  • Approach 3− Using list comprehension.

Approach 1− Using replace () method

Example

# Example 1: Removing a specific character from a string using str.replace()
string = "Hello$ world$"
new_string = string.replace ("$", "")
print(new_string)

Output

Hello world

Approach 1 uses str.replace () to remove a specific character from a string. The str.replace () method replaces a given character with a new character. Replacing the specific character with an empty string may aid in its removal. The str.replace () method replaces every instance of the mentioned character.

The variables in the example code stores the string "Hello$ world$". It requires removing the $ character present in it. We use the str.replace () method to replace the $ character with an empty string. The variable new_string receives the modified string. The print () function prints the modified string as the output eventually. The code outputs "Hello world". We removed the $ character from the original string. The program prints the modified string as the result.

Approach 2− Using translate () method

Example

# Example 2: Removing a specific character from a string using str.translate()
s = "Hello$ Python3$"
table = s.maketrans("", "", "$")
s1 = s.translate(table)
print(s1)

Output

Hello Python3

In Approach 2, we utilize the str.translate () method for eliminating a precise character from a string. The str.translate () method replaces specified characters in a dictionary or mapping table with a described character and returns the resulting string. This method enables us to eliminate a particular character from a string by using the str.translate() function that replaces specified characters with those in a dictionary or mapping table.

The code assigns the string "Hello$ Python3$" to variable s and identifies $ as the character that requires removal. The method str.maketrans () creates a translation table that maps the $ character to an empty string. The method str.translate () applies the translation table to the original string. The program assigns the modified string to the variable s1. The print () function is used to print the modified string.

The code produces the output "Hello Python3". We removed the $ character from the original string. The program prints the modified string as the output.

Approach 3− Using list comprehension.

Example

def remove (char, string_list):
   new_string = [string.replace (char, "") for string in string_list]
   return new_string

# Example usage
s = ["Hello", "World", "Python"]
character = "o"
output = remove (character, s)
print(output)

Output

['Hell', 'Wrld', 'Pythn']

Approach 3 defines a function named remove, which receives two parameters: char and string_list. We use the char parameter in Approach 3 to remove a specific character from every string in the string_list. We utilize list comprehension inside the function to generate a fresh list named new_string. We can construct a new list in a concise and efficient manner by utilizing list comprehension. The list comprehension statement iterates over each string in the string_list. We utilize the replace () method for every string, and substitute the specified character (char) with an empty string to eliminate it.

As we iterate through each string in string_list, we append the result of each replace () operation to the new_string automatically. This generates a novel list that comprises the modified strings with the removed specified character. As we iterate through each string in string_list and perform the replace () operation, the modified strings with the specified character removed are automatically added to a new list. Finally, the function produces and gives back the modified strings in the new_string.

The list of strings ["Hello", "World", "Python"] is present in the example usage section. Our goal is to eliminate the character "o" from every string. The remove function receives the list of strings ["Hello", "World", "Python"] and a character ("o") that needs to be removed, passed by us. The function uses list comprehension to iterate through every string present in the list, and it removes the character "o" by using the replace () method. While generating each modified string, it automatically includes them in the new_string. The function adds each modified string to the new_string automatically as it generates them.

['Hell', 'Wrld', 'Pythn'] is the output produced by the code. We removed the character "o" from each string in the list, resulting in modified strings stored in a new list. The code produces a new list that stores the modified strings obtained after removing the character "o". Approach 3 generates and delivers a fresh list consisting of edited strings after removing the specified character from each string through list comprehension.

Conclusion

We explored various techniques to eliminate a specified character from a string in Python throughout this article. The str.replace () and str.translate () methods stand out as the most commonly used techniques for eliminating characters from strings in Python. We specify a value and both methods replace a character with it. Specifying an empty character removes the character without any replacement. We also witnessed a list comprehension approach. It effectively eliminates the specified character from every string in the list by utilizing list comprehension, resulting in a new collection of altered strings. Depending on your preference and the specific requirements of your task, you can choose the approach that best suits your needs.

Updated on: 09-Oct-2023

145 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements