Connecting Tech Pros Worldwide Help | Site Map

Removing substring from string

Newbie
 
Join Date: Nov 2009
Posts: 1
#1: 3 Weeks Ago
I am trying to remove a specific substring from a string... Here are the doctests that are supposed to pass. I'm just absolutely stumped. Can someone point me in the right direction on where to start?

def remove(sub, s):

"""
>>> remove('an', 'banana')
'bana'
>>> remove('cyc', 'bicycle')
'bile'
>>> remove('iss', 'Mississippi')
'Mippi'
"""


def remove_all(sub, s):
"""
>>> remove('an', 'banana')
'ba'
>>> remove('cyc', 'bicycle')
'bile'
>>> remove('iss', 'Mississippi')
'Mippi'
"""


if __name__ == '__main__':
import doctest
doctest.testmod()
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#2: 3 Weeks Ago

re: Removing substring from string


I don't understand the question. Surely it's not as simple as this:
Expand|Select|Wrap|Line Numbers
  1. >>> 'banana'.replace('an', '')
  2. 'ba'
  3. >>> 'banana'.replace('an', '', 1)
  4. 'bana'
  5. >>> 'Mississippi'.replace('iss', '')
  6. 'Mippi'
  7. >>> 'Mississippi'.replace('iss', '', 1)
  8. 'Missippi'
  9. >>> 
Reply