Skip to content
Advertisement

Formatting a list of sentences extracted from text

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:

words = ['billion']
sentences = [sentence for sentence in text_1 if any(
    w.lower() in sentence.lower() for w in words)]

print(sentences)

Image of current output:

enter image description here

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:

for s in sentences:
    print(s.replace('n', ''))
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement