0

I am trying to remove a specific substring from a string... Here are the doctests. 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()

4
Contributors
6
Replies
9
Views
7 Years
Discussion Span
Last Post by vegaseat
Featured Replies
  • 1

    If [B]remove('an', 'banana') --> 'bana'[/B] then this should be [B]remove('iss', 'Mississippi') --> 'Missippi'[/B] So you can use ... [code]def remove(sub, s): # replace first sub with empty string return s.replace(sub, "", 1) # test print( remove('an', 'banana') ) # --> bana print( remove('iss', 'Mississippi') ) # --> Missippi [/code] If … Read More

0

A possible implementation of remove_all

import re

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

    return re.sub(re.escape(sub), '', s)


if __name__ == '__main__':
    import doctest
    doctest.testmod()

Note that a regex is compiled on each call.

Edited by Gribouillis: n/a

0

Thank you!! would you mind explaining why the implementation of return re.sub(re.escape(sub), '', s) works for the remove_all function specifically?

0

In fact, there is a simpler method

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

if __name__ == '__main__':
    import doctest
    doctest.testmod()

I don't understand your question, re.sub replaces every occurrence of a pattern by a given string, that's how it works.

0

oh ok... the book I am going by gave me these problems when I haven't learned about re.sub yet. Ive looked back and it was not even mentioned.

0

you could also try this:

def remove( s, rem ):
    newS = ""
    i = 0
    while i < len( s ):
        if s[ i : i + len( rem ) ] == rem:
            i += len( rem )
        else:
            newS += s[ i ]
            i += 1
    print newS
    
s = "Mississippi"; r = "iss"
remove( s, r )

the old facion way :)

1

If
remove('an', 'banana') --> 'bana'
then this should be
remove('iss', 'Mississippi') --> 'Missippi'
So you can use ...

def remove(sub, s):
    # replace first sub with empty string
    return s.replace(sub, "", 1)

# test
print( remove('an', 'banana') )        # --> bana
print( remove('iss', 'Mississippi') )  # --> Missippi

If you want to remove all subs, then use -1 in replace() which actually is the default value ...

def remove_all(sub, s):
    # replace all sub with empty string
    return s.replace(sub, "", -1)

# test
print( remove_all('an', 'banana') )        # --> ba
print( remove_all('iss', 'Mississippi') )  # --> Mippi

Python comes with a wealth of thoroughly tested string functions, might as well use them.

Edited by vegaseat: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.