I have a list of strings that I am iterating through. With each element/iteration I am trying to take the string and remove all non-alphabetic characters(numbers, punctuations, spaces, and more). However, when using a nested for-loop to access the individual characters and using replace function it is not working.
import string
new_war = ['I am the new king of Mississippi', 'next one you know', 'i got a family to feed']
print(new_war)
alphabet = string.ascii_uppercase + string.ascii_lowercase
def remove_char(lst):
for word in lst: #takes the individual string/element from the list
for chars in word: #goes through each individual character in the word element
if chars not in alphabet: #check to see if that character is in the alphabet(upper and lower)
word = word.replace(chars, "") #should take the given string, remove all occurrences of that char in the string via "",
remove_char(new_war)
charsshould be singular.word = word.replace(chars, "")This assigns to a new variable namedword. It does not modify the original list entry.