| by Arround The Web | No comments

How Python Functions Remove Characters From A String

Python is a popular and versatile programming language that offers multiple features and functionalities. Python programmers encounter several problems while manipulating strings, which are sequences/collections of characters. To remove certain characters from a string, either at the beginning, in the end, or in the middle, various inbuilt functions are used in Python.

How to Remove/Delete Characters From a String?

The following methods are employed in Python to remove or delete characters from a string:

Method 1: Remove String Characters Using the “replace()” Method

The “replace()” method is utilized in Python to replace the particular characters from the defined string with another particular character. This method can also be utilized to eliminate a character from a particular string.

Syntax

string.replace(oldvalue, newvalue, count)

In the above syntax:

  • The “oldvalue” parameter indicates the search string.
  • The “newvalue” parameter specifies the replacement string.
  • The “count” parameter represents the value that shows how many occurrences in the string need to be replaced.

Example

The following code is used to remove/delete the characters from the particular Python string:

str1 = "Hello and Welcome!"
print ("Original string: " + str1)
print ("\nRemoving all Occurrences:",str1.replace('o', ''))
print ("\nRemoving 1st Occurrences:",str1.replace('o', '', 1))

In the above code, the “replace()” method takes the string and “occurrence count” value as its arguments and deletes the character from the string accordingly. If no count occurrence value is given, all the occurrences of the specified character are removed from the string.

Output

Method 2: Remove String Characters Using the “translate()” Method

In Python, the “translate()” method is utilized to retrieve a string that contains particular characters that have been replaced with characters described in dictionaries or mapping tables. This method can also be employed to remove/delete the character from the Python string.

Syntax

string.translate(table)

In the above syntax, the “table” parameter refers to the “dictionary” or “mapping table” indicating where to replace it.

Example

The below code snippet removes the specified character from a string:

str1 = 'Hello and Welcome!'
print('Given String: ', str1)
print('After Removing Character:', str1.translate({ord(i): None for i in 'o'}))

In the above code, the “translate()” method takes the specified dictionary as a parameter and returns the string by removing the specified character from the input string.

Output

Method 3: Remove String Characters Using the “re.sub()” Function

The “re.sub()” function of the “re” module is utilized to replace every instance of the pattern in the input string. This can also be employed to remove/delete the characters from a string.

Syntax

re.sub(pattern, repl, string, count=0, flags=0)

In the above syntax:

  • The “pattern” parameter specifies the string/character that is going to be replaced.
  • The “repl” parameter refers to the new string that replaces/substitutes the old string.
  • The “string” indicates the input string in which the operation is performed.
  • The “flags” parameter is used to shorten the code.

Example

This code uses the “re.sub()” method to remove the character from the string:

import re
str1 = 'Hello and Welcome!'
print('Given String: ', str1)
print('After Removing Character:', re.sub("o","",str1))

In the above code, the “re.sub()” function of the “re” module takes the specified character, empty string, and the initialized string as its arguments, respectively, and removes the specified character from the string.

Output

Method 4: Remove String Characters Using the “for” loop

The “for” loop can also be utilized in Python to delete/remove the specified character from the string:

Example

This code block is utilized to eliminate the specified characters from a string via iteration:

str1 = "Hello and Welcome!"
print('Given String: ', str1)
str2 = ""
for i in range(0, len(str1)):
    if i != 1:
        str2 = str2 + str1[i]
print('After Removing Character:', str2)

According to the above code:

  • The original string and empty string are initialized, respectively.
  • The “for” loop iterates along the specified range and removes the stated character from the string based on the index/position value.

Output

Conclusion

The “replace()” method, “translate()” method, “re.sub()” method, or the “for” loop is used to remove/delete the characters from the initialized string. This Python tutorial presented various ways to remove characters from a string using relevant examples

Share Button

Source: linuxhint.com

Leave a Reply