Joshua of X Joshua of X - 1 month ago 6
Python Question

create new line in string on occurrence of word (trivial)

Hello I have a sentence such as:

<s>the cat</s><s>sat on a mat</s><s>he wore a hat</s>


I want:

<s>the cat</s>
<s>sat on a mat</s>
<s>he wore a hat</s>


I tried:

thisString.split("</s>")


this works but it deletes the
</s>
and removes whitespace (i want to keep both)

sorry for the trivial question but I cannot find a solution

Answer

.split("</s>")

Will replace </s> and split each occurance into a list.

I believe you would want .replace().

line = '<s>the cat</s><s>sat on a mat</s><s>he wore a hat</s>'
line = line.replace('</s>', '</s>\n')
print (line)

This will replace each </s> with the same tag but a newline at the end.

The output would be:

<s>the cat</s>
<s>sat on a mat</s>
<s>he wore a hat</s>