string.replace() returns the string with the replaced values. It doesn't modify the original so do something like this:
link['href'] = link['href'].replace("..", "")
Answer from joel goldstick on Stack Overflowstring.replace() returns the string with the replaced values. It doesn't modify the original so do something like this:
link['href'] = link['href'].replace("..", "")
string.replace() returns a copy of the string with characters replaced, as strings in Python are immutable. Try
s = link['href'].replace("..", '')
url=urljoin(page, s)
Str.replace not working?
python - String replacement not working - Stack Overflow
Does the character being replaced in .replace() have to be nonempty?
Struggling with replace method
So I was doing some basic coding challenges and one of them was to replace the first letter with the last letter (pretty simple right?) for some strings. I’ll give only 1 string for this example.
Edit - idk how to indent on Reddit sorry
str = “Chocolate”
Expected output = “ehocolatC”
So I wrote —
front = str[:1] back = str[-1:]
str.replace(back,front) str.replace(front,back)
output = “ChocolatC”
(So now if you are noobie like me your first thought was “oh yeah it’s because once you call the first replace function because back turns into a C the second replace function will replace a C with a C”)
So then I added a temp
front = str[:1] back = str[-1:]
tempBack = str[-1:] str.replace(back,front) str.replace(front,tempBack)
output = “ChocolatC”
Idk why please help a noob out Btw I even tested it by printing tempBack out right before replace(front,tempBack) and the output was an e!!! Tf?
I am trying to clean my twitter_handle column where some of the names have ?langen at the end of them. This is what I tried...
updated['twitter_handle'] = updated['twitter_handle'].str.replace('?langen', '', regex=True)re.error: nothing to repeat at position 0
From http://www.tutorialspoint.com/python/string_replace.htm
The method replace() returns a copy of the string in which the occurrences of old have been replaced with new
Try sandwich = sandwich.replace(restaurant, "")
string replacement returns a copy.
sandwich = sandwich.replace(restaurant, "")