multi_replace - Utilities

Skip to content
Back to top
View this page
Edit this page

# multi_replace

sage.misc.multireplace.multiple_replace(dic, text)

  Replace in ‘text’ all occurrences of any key in the given dictionary by its corresponding value. Returns the new string.

  EXAMPLES:

  Sage

      sage: from sage.misc.multireplace import multiple_replace
      sage: txt = "This monkey really likes the bananas."
      sage: dic = {'monkey': 'penguin', 'bananas': 'fish'}
      sage: multiple_replace(dic, txt)
      'This penguin really likes the fish.'

  Python

      >>> from sage.all import *
      >>> from sage.misc.multireplace import multiple_replace
      >>> txt = "This monkey really likes the bananas."
      >>> dic = {'monkey': 'penguin', 'bananas': 'fish'}
      >>> multiple_replace(dic, txt)
      'This penguin really likes the fish.'