Skip to content Skip to sidebar Skip to footer

The Replace() Method Won't Replace

I made a function to check if string is a palindrome It is suppose to replace anything in the string that is not an alphabet with '' but the replace() method isn't working alphabet

Solution 1:

Reassign it.

# word.replace(l, ''): the result of the function is a string,# so you must reassign
word = word.replace(l, '')

Solution 2:

To expand on HK boy's answer (and because I don't have enough reputation to comment), the replace method doesn't modify the existing string, it returns a new string. You have to assign it to a variable (or the same variable) in order to use the string with the replaced characters.

word = word.replace(l, '')

Post a Comment for "The Replace() Method Won't Replace"