Please support our Python advertiser: Programming Forums
Views: 366 | Replies: 5
![]() |
Can anyone tell me if there is any function for replacing multiples substrings within a string.
For instance we have this function call to replace multiple appearances of the same substring:
Here I replace all appearances of ';' with '_'
But what I need is a function that receives a list of substrings to replace withing a string.
Here I replace all appearances of ';', ',' and '.' with '_'
regards,
bc.
For instance we have this function call to replace multiple appearances of the same substring:
python Syntax (Toggle Plain Text)
newLine = line.replace(';', '_')
But what I need is a function that receives a list of substrings to replace withing a string.
python Syntax (Toggle Plain Text)
newLine = line.replace([';', ',', '.'], '_')
regards,
bc.
I don't know if there's a specific function to do it, but you could do a simple for loop like:
Or roll your own little function ie,
Same function, using list instead of string of chars
python Syntax (Toggle Plain Text)
>>> l = 'hi;your,face.is;on,fire' >>> for c in [';',',','.']: ... l = l.replace( c, '_' ) ... >>> l 'hi_your_face_is_on_fire' >>>
Or roll your own little function ie,
python Syntax (Toggle Plain Text)
>>> l = 'hi;your,face.is;on,fire' >>> def replaces( inp, old, new ): ... for c in old: inp = inp.replace( c, new ) ... return inp ... >>> l = replaces(l, ';,.', '_') >>> l 'hi_your_face_is_on_fire' >>>
Same function, using list instead of string of chars
python Syntax (Toggle Plain Text)
>>> l = 'hi;your,face.is;on,fire' >>> l = replaces(l, [ ';', ',', '.' ], '_') >>> l 'hi_your_face_is_on_fire' >>>
Last edited by jlm699 : Sep 30th, 2008 at 4:19 pm.
Let's Go Pens!
** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
If you don't mind learning regex, which is almost another language, but used by many popular computer languages for text manipulation:
python Syntax (Toggle Plain Text)
import re p = re.compile(r'[;,.]') s = 'hi;your,face.is;on,fire' print p.sub('_', s) # hi_your_face_is_on_fire
No one died when Clinton lied.
•
•
•
•
If you don't mind learning regex, which is almost another language
Regex it is just that, regular expression. A pattern describing some text.
Last edited by Aia : Oct 1st, 2008 at 3:00 pm.
At the very moment that I find myself in the side of the mayority, I will know that I need to re-think my ideas. ~ In my book.
Yes we know! How about learning some Python first before giving primitive lectures. We are trying to help people and keep a friendly atmosphere here!
BTW, if you need help with the module re, simply type help(re). Or go to this Python expert:
http://www.amk.ca/python/howto/regex/
BTW, if you need help with the module re, simply type help(re). Or go to this Python expert:
http://www.amk.ca/python/howto/regex/
Last edited by ZZucker : Oct 1st, 2008 at 5:49 pm.
Never argue with idiots, they'll just bring you down to their level and beat you with their experience.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode