[SOLVED] Formatting a list of sentences extracted from text

Issue

This Content is from Stack Overflow. Question asked by nji1997

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



Solution

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', ''))


This Question was asked in StackOverflow by nji1997 and Answered by danmohad It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?