-2

I need to split a string in half which I've already done with:

firstpart, secondpart = string[:len(string)//2], string[len(string)//2:]

I need it to split at a line break and I'm too new at coding to know how to approach this. Any tips would help.

Bird Leaf
  • 1
  • 2
  • Does this answer your question? [How to split a string of space separated numbers into integers?](https://stackoverflow.com/questions/6429638/how-to-split-a-string-of-space-separated-numbers-into-integers) – pppery Jun 13 '20 at 15:29
  • 4
    Which do you want, to split it in _half_, or to split at a line break? – khelwood Jun 13 '20 at 15:30
  • In half at the nearest line break. – Bird Leaf Jun 13 '20 at 15:32
  • 2
    Give as an example of the input and output. – MarianD Jun 13 '20 at 15:34
  • @BirdLeaf Please provide input example and explicitly show how you want to split it. A concrete example will help people to answer your question accurately. – Eisa Jun 13 '20 at 15:40
  • I'm trying to split song lyrics so it would turn this: Every day, the hours pass by So quickly, yet I don't do a thing I must be dumb I've been feelin' numb I can't explain, why nothing's gettin' done Please don't write this off, like you did the last 12 years I need a break, I need to breath I bet I need a thousand things So it here it goes, another chance Giving up at last love's first glance of real life You know that I don't handle pressure well Everyone would like to think it's Possible without having to think In half to send it in two separate messages. – Bird Leaf Jun 13 '20 at 15:47
  • Formatting screwed up but genius page is https://genius.com/Ovens-dead-as-fuck-lyrics – Bird Leaf Jun 13 '20 at 15:48

3 Answers3

0

Assuming the string has only one line break.

That would be:

firstpart, secondpart = string.split('\n')
Edison
  • 870
  • 1
  • 13
  • 28
  • My main issue is that there is multiple line breaks and I dont know how to distinguish which one to split at. – Bird Leaf Jun 13 '20 at 15:36
0

you can use the splitlines method which will work perfectly in your case,

str1="hope\n this helps\n you"
print(str1.splitlines())

output:

['hope', ' this helps', ' you']

It returns a list of splitted string.

Hope this helps you!

Community
  • 1
  • 1
Prathamesh
  • 1,064
  • 1
  • 6
  • 16
  • you're welcome it has been my pleasure to help you out! – Prathamesh Jun 13 '20 at 15:53
  • Comments attached to the question say that the requirement is to split "In half at the nearest line break." This splits into a list of as one element per line (three in this example). – alani Jun 13 '20 at 16:00
0

Try something like this:

mystring = """Mae hen wlad fy nhadau yn annwyl i mi,
Gwlad beirdd a chantorion, enwogion o fri;
Ei gwrol ryfelwyr, gwladgarwyr tra mad,
Dros ryddid collasant eu gwaed.

Gwlad!, GWLAD!, pleidiol wyf i'm gwlad.
Tra mor yn fur i'r bur hoff bau,
O bydded i'r hen iaith barhau.

Hen Gymru fynyddig, paradwys y bardd,
Pob dyffryn, pob clogwyn, i'm golwg sydd hardd;
Trwy deimlad gwladgarol, mor swynol yw si
Ei nentydd, afonydd, i fi.
"""

# get the half-way index
halfway = len(mystring) // 2

# get the indices of the nearest \n characters before and after the halfway
try:
    next_one = mystring.index("\n", halfway)
except ValueError:
    next_one = None

try:
    previous_one = mystring.rindex("\n", 0, halfway)
except ValueError:
    previous_one = None

# if no \n found at all, raise an error
if next_one == None and previous_one == None:
    raise ValueError

# or if a \n is only found on one side of halfway, use that one
elif next_one == None:
    pos = previous_one

elif previous_one == None:
    pos = next_one

# or if it is found on both sides of half-way, use whichever is nearer
elif next_one - halfway < halfway - previous_one:
    pos = next_one

else:
    pos = previous_one

# now actually split the string
part1 = mystring[:pos]
part2 = mystring[pos + 1:]

print("FIRST HALF:", part1)
print("==========")
print("SECOND HALF:", part2)

Gives:

FIRST HALF: Mae hen wlad fy nhadau yn annwyl i mi,
Gwlad beirdd a chantorion, enwogion o fri;
Ei gwrol ryfelwyr, gwladgarwyr tra mad,
Dros ryddid collasant eu gwaed.

Gwlad!, GWLAD!, pleidiol wyf i'm gwlad.
==========
SECOND HALF: Tra mor yn fur i'r bur hoff bau,
O bydded i'r hen iaith barhau.

Hen Gymru fynyddig, paradwys y bardd,
Pob dyffryn, pob clogwyn, i'm golwg sydd hardd;
Trwy deimlad gwladgarol, mor swynol yw si
Ei nentydd, afonydd, i fi.

alani
  • 12,573
  • 2
  • 13
  • 23