| | |
Removing a substring from string
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
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 Nov 2nd, 2009
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; Nov 2nd, 2009 at 5:11 pm.
0
#4 Nov 2nd, 2009
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 Nov 3rd, 2009
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 Nov 3rd, 2009
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; Nov 3rd, 2009 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
Views: 471 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied alarm aliased basic beginner casino character code corners cx-freeze definedlines development dictionary digital dynamic editing error events examples excel exe file filename float format ftp function graphics gui handling homework ideas iframe import input java line linux list lists logging loop matching mouse newb number numbers numeric output parameters parsing path port prime program programming progressbar projects py py2exe pygame pyglet pyqt pysimplewizard python random recursion recursive return reverse schedule scrolledtext searchingfile shebang skinning sprite ssh statistics stdout string strings table tails terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 variable voip windows wxpython






