How can I replace certain string in a string in Python?

2024/10/11 20:31:29

I am trying to write two procedures to replace matched strings in a string in python. And I have to write two procedures.

def matched_case(old new): .........

note: inputs are two strings, it returns a replacement converter.

def replacement(x,another_string): ..........

note:inputs are a converter from previous procedure, and a string. It returns the result of applying the converter to the input string.

for example:

a = matched_case('mm','m')
print replacement(a, 'mmmm')
it should return m

another example:

R = matched_case('hih','i')
print replacement(R, 'hhhhhhihhhhh')
it should return hi

I am not sure how can I use loop to do the whole thing. Thanks so much for anyone can give a hint.

Answer
def subrec(pattern, repl, string):while pattern in string:string = string.replace(pattern, repl)return string

foo('mm', 'm', 'mmmm') return m

foo('hih', 'i', 'hhhhhhihhhhh') return hi

https://en.xdnf.cn/q/118280.html

Related Q&A

Python: `paste multiple (unknown) csvs together

What I am essentially looking for is the `paste command in bash, but in Python2. Suppose I have a csv file:a1,b1,c1,d1 a2,b2,c2,d2 a3,b3,c3,d3And another such:e1,f1 e2,f2 e3,f3I want to pull them toget…

Django Redirect after Login Error

Im new to Django and I know that to redirect after login I have to set the parameter page. But this only works when the login is successful. How can i do the same thing when some error occurs?? Ps: I…

Python: Pulling .png from a website, outputting to another

Hoping this question is not too vague, or asking for too much. Essentially I am analyzing large amounts of spectra, and wanting to create one large webpage that contains these spectra rather than looki…

Using peakutils - Detecting dull peaks

Im currently using peakutils to find peaks in some data. My data contains some "dull peaks", that is my peaks plateau somewhat. I cant set my code to find these peaks even after playing aroun…

nonlinear scaling image in figure axis matplotlib

enter image description hereI hope I have not over-looked as previously asked question. I dont think so. I have an image of a spectrum. I have several laser lines for calibration. Since the laser li…

How to correctly call a git submodule symlinked?

On the Sublime Text Package Control issue:Why ignore VCS-based packages accordingly to this message? I find out what causes this error. I had the package All Autocomplete triggering it. Then I gone to…

Python and MySQL query with quotes

With a script in Python3, after extracting some strings from a file, they should be used as data to be inserted into a MySQL database as follows:query1 = """INSERT INTO {:s} VALUES ({:s}…

Using scipy kmeans for cluster analysis

I want to understand scipy.cluster.vq.kmeans. Having a number of points distributed in 2D space, the problem is to group them into clusters. This problem came to my attention reading this question and …

Scrapy and celery `update_state`

I have the following setup (Docker):Celery linked to Flask setup which runs the Scrapy spider Flask setup (obviously) Flask setup gets request for Scrapy -> fire up worker to do some workNow I wish …

SPIDEV on raspberry pi for TI DAC8568 not behaving as expected

I have a Texas Instruments DAC8568 in their BOOST breakout board package. The DAC8568 is an 8 channel, 16bit DAC with SPI interface. The BOOST package has headers to connect it to my raspberry pi, an…