LIMITED TIME OFFER: 50% OFF OF PREMIUM WITH OUR ANNUAL PLAN (THAT'S $294 IN SAVINGS).
GET OFFER

Trying to understand why do I have to assign a variable inside a for loop

Screen Link:
https://app.dataquest.io/m/351/cleaning-and-preparing-data-in-python/7/parsing-numbers-from-complex-strings-part-one

My Code:

test_data = ["1912", "1929", "1913-1923",
             "(1951)", "1994", "1934",
             "c. 1915", "1995", "c. 1912",
             "(1988)", "2002", "1957-1959",
             "c. 1955.", "c. 1970's", 
             "C. 1990-1999"]

bad_chars = ["(",")","c","C",".","s","'", " "]

def strip_characters(string):
    
    for char in bad_chars:
        string.replace(char,'')
    return string
        
stripped_test_data = []

for year in test_data:
    stripped_test_data.append(strip_characters(year))

What I expected to happen:
I expected to return the test_data list with cleaned strings.

What actually happened:

 stripped_test_datalist (<class 'list'>)
- actual + expected

  ['1912',
   '1929',
   '1913-1923',
-  '(1951)',
+  '1951',
   '1994',
   '1934',
-  'c. 1915',
+  '1915',
   '1995',
-  'c. 1912',
-  '(1988)',
+  '1912',
+  '1988',
   '2002',
   '1957-1959',
-  'c. 1955.',
-  "c. 1970's",
-  'C. 1990-1999']
+  '1955',
+  '1970',
+  '1990-1999']

To summarize: the answer code assign string.replace(char,’’) to a variable “string” inside the for loop:

for char in bad_chars:
    string = string.replace(char,'')
return string

Why do I have to do this assignment inside the for loop? Why is it not possible to simply use string.replace(char,’’) directly whitout assign it to a variable string? As I understand, if I just keep string.replace(char,’’), every time I run the loop the string wiil be updated replacing one of the bad characters in the bad characters list, one at a time.

After some reflection I concluded that if a don’t assign it to a variable, it will return only the replacement of the last character. Is that correct?

Not quite correct.

If you don’t assign it back to the same variable, then the changes aren’t saved. So, if you have the string (2001), you would get back the same string (2001) because any replacement you carried out wasn’t stored anywhere.

It’s clear now. Thank you!