I am trying to format a list neatly that I have extracted using regex. I would like to have each sentence in its own line and remove the n
characters:
JavaScript
x
6
1
words = ['billion']
2
sentences = [sentence for sentence in text_1 if any(
3
w.lower() in sentence.lower() for w in words)]
4
5
print(sentences)
6
Image of current output:
Advertisement
Answer
From OP’s image, text_1
is a list of strings. To remove the newline n
characters from a string, you can use the string’s replace
method. To print each newline-removed sentence on its own line, you can use a simple for
loop. Keeping the rest of the code intact, replace print(sentences)
with:
JavaScript
1
3
1
for s in sentences:
2
print(s.replace('n', ''))
3