Skip to content Skip to sidebar Skip to footer

Python: Why Does Replace Not Work?

I wrote a quick script to remove the 'http://' substring from a list of website addresses saved on an excel column. The function replace though, doesn't work and I don't understand

Solution 1:

String.replace(substr)

does not happen in place, change it to:

string = string.replace("http://","")

Solution 2:

string.replace(old, new[, max]) only returns a value—it does not modify string. For example,

>>>a = "123">>>a.replace("1", "4")
'423'
>>>a
'123'

You must re-assign the string to its modified value, like so:

>>>a = a.replace("1", "4")>>>a
'423'

So in your case, you would want to instead write

string = string.replace("http://", "")

Post a Comment for "Python: Why Does Replace Not Work?"