Python Forum
How to create new line '/n' at each delimiter in a string? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to create new line '/n' at each delimiter in a string? (/thread-31487.html)



How to create new line '/n' at each delimiter in a string? - MikeAW2010 - Dec-14-2020

I want python to automatically import a new line, or /n code at each delimiter within a string. Lets say I want Python to make a new line at every 'period' it sees within the string...WITHOUT typing out the /n myself (I want Python to automatically import the new line)

Lets say I have a string

Quote:'I want to be a developer. I also want to learn AWS. I am going to practice joining strings.'

And at each period within the string, I want Python to create a new line, so it reads like this:

Quote:'I want to be a developer.
I also want to learn AWS.
I am going to practice joining strings.'

I know the split function can split the string, but I cant figure out how to import the /n code into each delimiter, or how to import new lines in python without coding it. How can I do this automatically rather than manually adding a /n to each line?


RE: How to create new line '/n' at each delimiter in a string? - bowlofred - Dec-15-2020

This is slightly tricky, because at the end you only have a ".", but between the sentences you have ". ". So a simple substitution makes it look a bit wonky.

So I might first replace all ". " with ".", then replace those with ".\n".

s = 'I want to be a developer. I also want to learn AWS. I am going to practice joining strings.'
s = s.replace(". ", ".")
s = s.replace(".", ".\n")
print(s)
Output:
I want to be a developer. I also want to learn AWS. I am going to practice joining strings.



RE: How to create new line '/n' at each delimiter in a string? - perfringo - Dec-15-2020

This could be done using split, construct and join.

- split string at delimiter
- construct stream of substrings with putting delimiter back
- join substrings using repalcement string

There are corner cases which must be tested/addressed but something along those lines is sufficient to start:

def formatter(text, delimiter='.', replacement='\n'):
    stream = (f'{token.strip()}{delimiter}' for token in text.split(delimiter) if
              token) 
    return replacement.join(stream)


s = 'I want to be a developer. I also want to learn AWS. I am going to practice joining strings.'

print(formatter(s)) ->
I want to be a developer.
I also want to learn AWS.
I am going to practice joining strings.



RE: How to create new line '/n' at each delimiter in a string? - snippsat - Dec-15-2020

Regex can be useful for this if need to build it with more rules.
Example let say it's ? instead of .,it should normally be new line there to.
I have done this before for a subtext parser i wrote,i had to add many rules that would have been very difficult to write with just string methods and loops.
import re

>>> s = 'I want to be a developer. I also want to learn AWS. I am going to practice joining strings.'
>>> result = re.sub(r'\.\s', r'\n', s)
>>>
>>> print(result)
I want to be a developer
I also want to learn AWS
I am going to practice joining strings.
So if get a new text with a new problem,can build on regex or just pass text to a new re.sub() if regex get to complex in one go.
>>> import re 
>>> 
>>> s = 'I want to be a developer. I also want to learn AWS. What If I Don’t Know If the Woman Is Married or Not? hello world a new line'
>>> result = re.sub(r'\.\s|\?\s', r'\n', s)
>>>
>>> print(result)
I want to be a developer
I also want to learn AWS
What If I Don’t Know If the Woman Is Married or Not
hello world a new line