Python Forum
How to create new line '/n' at each delimiter in a string?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create new line '/n' at each delimiter in a string?
#1
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?
Reply
#2
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.
LastStopDEVS and sridhar like this post
Reply
#3
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.
sridhar likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to capture string from a line to certain line jerald 1 242 Jun-30-2021, 05:13 PM
Last Post: Larz60+
  How to print string multiple times on new line ace19887 7 1,326 Sep-30-2020, 02:53 PM
Last Post: buran
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 547 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  Add new line after finding last string in a region Nigel11 1 586 Aug-08-2020, 10:00 PM
Last Post: Larz60+
  Create array from string steve87bg 4 843 Jul-13-2020, 07:55 PM
Last Post: jefsummers
  copy content of text file with three delimiter into excel sheet vinaykumar 0 559 Jul-12-2020, 01:27 PM
Last Post: vinaykumar
  Searching string in file and save next line dani8586 2 664 Jul-10-2020, 09:03 AM
Last Post: dani8586
  Finding line numbers starting with certain string Sutsro 3 844 Jun-27-2020, 12:36 PM
Last Post: Yoriz
  create an array of each line of text macieju1974 7 1,042 Jun-07-2020, 06:30 PM
Last Post: Yoriz
  How to print string multiple times separated by delimiter Mekala 1 697 May-23-2020, 09:21 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020