Python Program to replace the string by specified character


Replacing characters in a string with specified characters is a common text-processing method with many different applications. There are some examples like Data transformation, text normalization, and data cleansing. In Python, we have some string in-built functions that can be used to convert a string into an array of characters based on the specified character. The group of characters to form a word is called a string. In this program, we need an empty string to store the new string in it.

Syntax

The following syntax is used in the examples −

replace()

The replace() is an inbuilt function used in Python to remove the specific character.

join()

This is an in-built function that joins all the items into one string.

re.sub()

The re is a module that supports regular expression. The sub() is an in-built function that can be used to replace the specified array of characters.

Example 1

In this program, we will start the program by storing the input string in the variable named strg. Then initialize the variable char_str to store the value by replacing the specific character i.e. ‘a’ and ‘e’. The replace() function acts as an object in the variable strg and it accepts two parameters- characters and empty string( “” ) will be storing the new string). Finally, we are printing the variable with the help of the variable char_str.

strg = "Black Diamond Red Carpet"
char_str = strg.replace('a',"").replace('e',"")
print("After removing the specified character:",char_str)

Output

After removing the specified character: Blck Dimond Rd Crpt

Example 2

In this program, we will start the program by storing the input string in the variable my_str. Then we use the list to iterate over each character of the string and check if it is not ‘a’ and ‘e’. Then join the filter character using the join() method and modify it to become a new string. Finally, print the variable with the help of the variable char_str.

my_str = "Ring O Rings"
char_str = "".join([char for char in my_str if char not in['g','O']])
print("After removing the specified character:",char_str)

Output

After removing the specified character: Rin  Rins

Example 3

In this program, we will start the program by importing the module named re that will verify the matching string. Then store the input string in the variable p_str. Next, the sub() function acts as an object with the module named re. The sub() function accepts three parameters- ‘[ml]’- to remove the character, “”- empty string to store the new string, p_str- given input string and store it in the variable chng_str. Next, print the result with the help of the variable chng_str.

import re
p_str = "malayalam"
chng_str = re.sub("[ml]", "", p_str)
print("After removing the specified character in the string:",chng_str)

Output

After removing the specified character in the string: aayaa

Conclusion

We discussed the three different in-built functions- sub(), join(), and replace() to remove the specified character in the string. In every example, it used the empty string to store the new string by replacing the specified character.

Updated on: 01-Jun-2023

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements