# Python Program to Solve the Replace and Remove Problem

#### Replace and Remove problem:

The name of the problem is Replace and Remove Problem, and we will be replacing one specific character with a different string as well as removing a specific character from the user’s input.

So we know we need to replace one character with another string or set of characters and remove one character from the input. The two rules that we will abide by are as follows:

1. ReplaceÂ aÂ with double d (dd)
2. Remove any occurrence of b

Examples:

Example1:

Input:

Given String = "pythonarticlesbinarybooks"

Output:

Original String =  pythonarticlesbinarybooks
The result string =  pythonddrticlesinddryooks

Example2:

Input:

Given string ="haaabbcdcj"

Output:

Original String = haaabbcdcj
The result string = hddddddbcdcj

## Program to Solve the Replace and Remove Problem in Python

Below are the ways to solve the replace and remove problem in Python:

Approach:

Convert the given string to list and Traverse the list check if the character is equal to ‘a’ if it is equal modify the list element with dd else check if the character is equal to ‘b’ then remove the element from the list.Join all elements of the list and print it.

### Method #1: Using For loop (Static Input)

Approach:

• Give the input string as static input and store it in a variable.
• Convert the given string to a list using the list() function and store it in a variable.
• Calculate the length of the list using the len() function and store it in a variable.
• Loop till the length of the given list using the for loop.
• Check if the list element at the iterator index is equal to a using the if conditional statement.
• If it is true then replace the list element at iterator index with ‘dd’.
• Loop in the above list using the For loop and in operator.
• Check if the element is equal to ‘b’ using the if conditional statement.
• If it is true then remove the element from the list using the remove() function.
• After the end of for loop.
• Convert the list to string using the join() function.
• Print the string after joining the list elements.
• The Exit of the Program.

Below is the implementation:

# Give the input string as static input and store it in a variable.
gvnstrng = 'pythonarticlesbinarybooks'
# Convert the given string to a list using the list() function and store it in a variable.
strnglist = list(gvnstrng)
# Calculate the length of the list using the len() function and store it in a variable.
lstlengt = len(strnglist)
# Loop till the length of the given list using the for loop.
for indx in range(lstlengt):
# Check if the list element at the iterator index is equal to a
# using the if conditional statement.
if(strnglist[indx] == 'a'):
# If it is true then replace the list element at iterator index with 'dd'.
strnglist[indx] = 'dd'
# loop in the above list using the For loop and in operator
for ele in strnglist:
# check if the element is equal to 'b' using the if conditional statement.
if(ele == 'b'):
# If it is true then remove the element from the list using the remove() function.
strnglist.remove(ele)

# After the end of for loop.
# Convert the list to string using the join() function.
rststrng = ''.join(strnglist)
# Print the string after joining the list elements.
print('Original String = ', gvnstrng)
print('The result string = ', rststrng)


Output:

Original String =  pythonarticlesbinarybooks
The result string =  pythonddrticlesinddryooks

### Method #2: Using For loop (User Input)

Approach:

• Give the input string as user input using list(),map(),split(),int() functions and store it in a variable.
• Convert the given string to a list using the list() function and store it in a variable.
• Calculate the length of the list using the len() function and store it in a variable.
• Loop till the length of the given list using the for loop.
• Check if the list element at the iterator index is equal to a using the if conditional statement.
• If it is true then replace the list element at iterator index with ‘dd’.
• Loop in the above list using the For loop and in operator.
• Check if the element is equal to ‘b’ using the if conditional statement.
• If it is true then remove the element from the list using the remove() function.
• After the end of for loop.
• Convert the list to string using the join() function.
• Print the string after joining the list elements.
• The Exit of the Program.

Below is the implementation:

# Give the input string as user input using list(),map(),split(),int() functions and store it in a variable.
gvnstrng = input('Give some random string = ')
# Convert the given string to a list using the list() function and store it in a variable.
strnglist = list(gvnstrng)
# Calculate the length of the list using the len() function and store it in a variable.
lstlengt = len(strnglist)
# Loop till the length of the given list using the for loop.
for indx in range(lstlengt):
# Check if the list element at the iterator index is equal to a
# using the if conditional statement.
if(strnglist[indx] == 'a'):
# If it is true then replace the list element at iterator index with 'dd'.
strnglist[indx] = 'dd'
# loop in the above list using the For loop and in operator
for ele in strnglist:
# check if the element is equal to 'b' using the if conditional statement.
if(ele == 'b'):
# If it is true then remove the element from the list using the remove() function.
strnglist.remove(ele)

# After the end of for loop.
# Convert the list to string using the join() function.
rststrng = ''.join(strnglist)
# Print the string after joining the list elements.
print('Original String = ', gvnstrng)
print('The result string = ', rststrng)


Output:

Give some random string = haaabbcdcj
Original String = haaabbcdcj
The result string = hddddddbcdcj