-1

I have this String:

Hello World.\n
I'm very happy today.\n\n\n\n\n
How are you?\n\n\n
Bye.

And I want to split it by two or more new lines without using any libraries. Output:

['Hello World.\n I'm very happy today','How are you?','Bye']
FasterSol
  • 19
  • 4
  • 1
    What's your desired output? – U13-Forward Dec 30 '20 at 08:30
  • Do you know how to split a string based on a delimiter? – Chris_Rands Dec 30 '20 at 08:31
  • what did you try? Did you try to use `for`-loop and count new lines ? – furas Dec 30 '20 at 08:32
  • `[x for x in s.split('\n') if x]` ? Or are there some special requirements? – Niko Föhr Dec 30 '20 at 08:32
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). "Show me how to solve this coding problem?" is off-topic for Stack Overflow. You have to make an honest attempt at the solution, and then ask a *specific* question about your implementation. Stack Overflow is not intended to replace existing tutorials and documentation. – Prune Dec 30 '20 at 08:32
  • `split` is a method of a built-in class, so you're not using any "libraries". Look up how to use it. – Prune Dec 30 '20 at 08:33
  • [split on 2 or more new lines - without libraries](https://pastebin.com/K4r0vAhn) - similar problems I made long ago in C/C++ as student. – furas Dec 30 '20 at 08:47

1 Answers1

0

Python's base string function split() should work here, without the need to import anything:

inp = "Hello World.\nI'm very happy today.\n\n\n\n\nHow are you?\n\n\nBye."
terms = inp.split('\n\n')
print(terms)

This prints:

["Hello World.\nI'm very happy today.", '', '\nHow are you?', '\nBye.']
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360