0
Sentence = "the heart was made to be broken"

How to split sentence for displaying in separate lines by using Python? (4 words per line)

Line1: the heart was made
Line2: to be broken

Any advice?

ThanaDaray
  • 1,693
  • 5
  • 22
  • 28
  • 1
    Do you just need a solution for this specific sentence, of do you have a more general problem? – wkl May 05 '12 at 17:04
  • Not for this specific sentence. I have more than 50 sentences. – ThanaDaray May 05 '12 at 17:05
  • I think I said at the above that I want to display 4 words per line. – ThanaDaray May 05 '12 at 17:06
  • do you count punctuations as separate words (i hope not) or do you count them as a part of a single word. e.g. do you mean "word." is a word? – Parth Thakkar May 05 '12 at 17:09
  • 1
    Your question is a modified version of this problem: http://stackoverflow.com/questions/1621906/is-there-a-way-to-split-a-string-by-every-nth-seperator-in-python – wkl May 05 '12 at 17:09

5 Answers5

6
  1. Turn the sentence into a list of words.
  2. Partition the list into chunks of 4 words.
  3. Combine the partitions into lines.
Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

Try this:

s = 'the heart was made to be broken'

for i, word in enumerate(s.split(), 1):
    if i % 4:
        print word,
    else:
        print word

> the heart was made
> to be broken
Óscar López
  • 232,561
  • 37
  • 312
  • 386
0

Here's a solution:

import math

def fourword(s):
    words = s.split()
    fourcount = int(math.ceil(len(words)/4.0))
    for i in range(fourcount):
        print " ".join(words[i*4:(i+1)*4])

if __name__=="__main__":
    fourword("This is a test of fourword")
    fourword("And another test of four")

The output is:

>python fourword.py 
This is a test
of fourword
And another test of
four
craigmj
  • 4,827
  • 2
  • 18
  • 22
0

Let me explain the solution for this problem which use itertools module. When you're trying to deal with sequence, be it a list or string or any other iterable, it's generally a good first step to take a look at itertools module from standard library

from itertools import count, izip, islice, starmap

# split sentence into words
sentence = "the heart was made to be broken".split()
# infinite indicies sequence -- (0, 4), (4, 8), (8, 12), ...
indicies = izip(count(0, 4), count(4, 4)) 
# map over indices with slicing
for line in starmap(lambda x, y: sentence[x:y], indicies):
    line = " ".join(line)
    if not line:
        break
    print line
andreypopp
  • 6,887
  • 5
  • 26
  • 26
0

Generic function:

from itertools import count, groupby

def split_lines(sentence, step=4):
    c = count()
    chunks = sentence.split()
    return [' '.join(g) for k, g in groupby(chunks, lambda i: c.next() // step)]

Which you can use like this:

>>> sentence = "the heart was made to be broken"
>>> split_lines(sentence)
['the heart was made', 'to be broken']
>>> split_lines(sentence, 5)
['the heart was made to', 'be broken']
>>> split_lines(sentence, 2)
['the heart', 'was made', 'to be', 'broken']

With the result you can do anything you want (including printing):

>>> for line in split_lines(sentence):
...     print line
...     
the heart was made
to be broken
rubik
  • 8,814
  • 9
  • 58
  • 88
  • 1
    Can you do this for a number of characters? –  Jul 27 '17 at 21:07
  • @BenWebb Yes, you can use a slightly modified version of the `split_lines` function above. Instead of operating on `chunks = sentence.split()` you operate on `sentence` directly. – rubik Jul 28 '17 at 07:31