Strings are immutable in python. You assign a new string with
convert = title.replace(i, '')
title
remains unchanged after this statement. convert
is an entirely new string that is missing i
.
On the next iteration, you replace a different value of i
, but still from the original title
. So in the end it looks like you only ran
convert = title.replace('+', '')
You have two very similar options, depending on whether you want to keep the original title
around or not.
If you do, make another reference to it, and keep updating that reference with the results, so that each successive iteration builds on the result of the previous removal:
convert = title
for i in blacklisted_chars:
convert = convert.replace(i, '')
print(convert)
If you don't care to retain the original title
, use that name directly:
for i in blacklisted_chars:
title = title.replace(i, '')
print(title)
You can achieve a similar result without an explicit loop using re.sub
:
convert = re.sub('[#|@+]', '', title)