| | |
Removing a substring from string
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 3
Reputation:
Solved Threads: 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()
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()
0
#2 25 Days Ago
A possible implementation of remove_all
Note that a regex is compiled on each call.
python Syntax (Toggle Plain Text)
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()
Last edited by Gribouillis; 25 Days Ago at 5:11 pm.
0
#4 25 Days Ago
In fact, there is a simpler method
I don't understand your question, re.sub replaces every occurrence of a pattern by a given string, that's how it works.
python Syntax (Toggle Plain Text)
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()
0
#6 24 Days Ago
you could also try this:
the old facion way
Python Syntax (Toggle Plain Text)
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
#7 24 Days Ago
If
remove('an', 'banana') --> 'bana'
then this should be
remove('iss', 'Mississippi') --> 'Missippi'
So you can use ...
If you want to remove all subs, then use -1 in replace() which actually is the default value ...
Python comes with a wealth of thoroughly tested string functions, might as well use them.
remove('an', 'banana') --> 'bana'
then this should be
remove('iss', 'Mississippi') --> 'Missippi'
So you can use ...
Python Syntax (Toggle Plain Text)
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
Python Syntax (Toggle Plain Text)
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
Last edited by vegaseat; 24 Days Ago at 10:27 am.
May 'the Google' be with you!
![]() |
Similar Threads
- how to put Dilimiter in first left substring in whole string (Visual Basic 4 / 5 / 6)
- removing spaces from a string (C++)
- String Class, substring function (C++)
- Removing Characters from a String (C++)
- removing spaces from string (C++)
- how to extract substring from a string? (C#)
- string and substring (C)
- Removing punctuation from a string (C++)
- Removing a substring from a string (Python)
Other Threads in the Python Forum
- Previous Thread: Parsing Problem
- Next Thread: File size is increased after pickle
| Thread Tools | Search this Thread |
abrupt alarm ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






