Censor_four strips out the newlines from the original email

Your solution function censor_four strips out the newlines from the original email when it creates a list with all the words:

def censor_four(input_text, censored_list):
input_text_words =
for x in input_text.split(" “):
x1 = x.split(”\n")
for word in x1:
input_text_words.append(word)

I was trying to figure out how to preserve the newlines in the word list before getting to the censoring code, so that when the text is re-joined after the censoring, it preserves the format of the original email with the newlines . I created two functions, the first to create a list with the email text split up by spaces, same as you, then a second function to parse that list and check each element for newlines (’\n’), and create a new list with the newlines as separate elements:

def text_to_list(text):
text_list = text.split(" ")
return text_list

def separate_newlines(text_list):
words =
for x in range(len(text_list)):
if ‘\n’ in text_list[x ]
i = text_list[x ].find(’\n’)
words.append(text_list[x ][0:i])
words.append(’\n’)
words.append(text_list[x ][i+2:])
else:
words.append(text_list[x ])
return words

I started with looking for one newline per element, and was going to expand on that code to find a second new line. But I found that even though elements in the first list have strings that correctly have the two newlines from email_four, for example ‘HELP!\n\nHelena’, when run through my separate_newlines function it appears to treat the first ‘\n’ as an escape sequence, and my new list has three elements ‘HELP’, ‘\n’,’ Helena’, rather than ‘HELP’, ‘\n’, ‘\nHelena’.

Is that to be expected, and is there a way to correctly recognize the first ‘\n’ as a newline and not an escape sequence?

There are some formatting problems with my post. Indentation is not showing up. Also [x ] shows up as without the extra space I entered. And the square when words is defined is really left and right square brackets.

There are buttons for formatting in the forum post editor

newline is less special than you might think. you can treat it the same as space and punctuation, a non-letter character.

1 Like