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()

Recommended Answers

All 6 Replies

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.

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

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.

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.

Member Avatar for masterofpuppets

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 :)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.