prateek prateek - 28 days ago 5
Python Question

Search and replace operation

I have a list which has URL values like:

http://farm6.static.flickr.com/5149/5684108566_aed8b9b52d_s.jpg


How can I change the
_s
in the end to
_m
for all occurrences?

Answer

Try this:

str = "http://farm6.static.flickr.com/5149/5684108566_aed8b9b52d_s.jpg"
str = str.replace("_s","_m")

If you want to be sure that only the las part is changed and you know all are .jpg files you can use:

str = "http://farm6.static.flickr.com/5149/5684108566_aed8b9b52d_s.jpg"
str = str.replace("_s.jpg","_m.jpg")

To give some more context and avoid changes on the middle of the url.