pythonstringfunctionreplacemutation

Python replace/remove substring from input string with a loop and mutating the original string with each iteration


I am looking to process an input (string) and remove any items that follow the pattern of being encased in square brackets e.g [like this].

I have been able to identify the pattern which allows me to isolate and create a list of all occurences of square brackets in the string. using regex.

However I would like to replace these occurances and mutate the original string each time to replace the squared brackets with "" to remove them from the original string.

As I understand string.replace("example","") returns a copy of the original string.

How would I go about a technique to remove each occurence and then return the mutated input string.

My code for this function so far, is as follows.

def remove_square_brackets(input):
  """remove square brackets e.g [verse] from song lyrics """
  pattern1 = r"\[.*?\]" 
  squares = re.findall(pattern1, input) #this works, here we have seperated by chorus verse. 
  #can also be used to remove these bookmarks from the phrases themselves. 
  print("squares are", squares) #this is picking up all the squares
  for square in squares:
        print(square)
        if square in input:
             print("found in the string!")
             #replace does not mutate, rather it makes a copy therefore this will not work,. 
             input.replace(square,"")
  return input

Solution

  • Reassign the changed string to the input variable inside the loop using input = input.replace(square, ""). This ensures that each occurrence of the brackets is removed one at a time from the input variable and that the final result is returned correctly.

    import re
    
    
    def remove_square_brackets(input):
        """Remove square brackets e.g [verse] from song lyrics."""
    
        # Define the regular expression pattern to match square brackets and the text inside them.
        pattern1 = r"\[.*?\]"
    
        # Find all occurrences of the pattern1 in the input string using regular expression.
        squares = re.findall(pattern1, input)
    
        # Iterate through each occurrence of square brackets in the input string.
        for square in squares:
            # Print the square bracket occurrence found in the input string.
            print(square, "found in string!")
    
            if square in input:
                # Replace the square bracket occurrence and reassign it to the input variable.
                input = input.replace(square, "")
    
        # Return the modified input string with square brackets removed.
        return input
    
    
    # Example usage:
    lyrics = "This is [verse] some [chorus] example [bridge] lyrics."
    result = remove_square_brackets(lyrics)
    print(result)
    

    this is the output:

    [verse] found in string!
    [chorus] found in string!
    [bridge] found in string!
    This is  some  example  lyrics.