tags:

views:

47

answers:

3

i have a string like this:

row='saint george 1739 1799 violin concerti g 029 039 050 symphonie concertante for two violins g 024 bertrand cervera in 024 039 christophe guiot in 024 029 and thibault vieux violin soloists orchestre les archets de paris'

i have this loop:

for n in range (1,int(len(row)/55)+1):
print row[(n-1)*55:n*55]

it is working well!!

however, it is cutting the spaces:

saint george 1739 1799 violin concerti g 029 039 050 sy
mphonie concertante for two violins g 024 bertrand cerv
era in 024 039 christophe guiot in 024 029 and thibault

i do not want it to cut the spaces (however i still want either 55 characters or less per line)

+3  A: 

have a look at textwrap module.

SilentGhost
Can `textwrap` break within word boundaries? Erm, wait, that's what the OP wants, I guess.. I just reread the question.
intuited
+1  A: 

I think the clearest way would be

for i in range(0, len(row), 55):
    print test[i:i+55]

where the third parameter to range is the step value, i.e. the difference between range elements.

edit: just reread your question, and realized that I'm not sure if you want it to break along word boundaries or to just do a hard break at the 55th character of each line.

If you do want it to break along word boundaries, you should check out the textwrap module as suggested by unutbu and SilentGhost.

intuited
@intuited: thank you very much. ive edited the question a little bit
i am a girl
+4  A: 
import textwrap

row='saint george 1739 1799 violin concerti g 029 039 050 symphonie concertante for two violins g 024 bertrand cervera in 024 039 christophe guiot in 024 029 and thibault vieux violin soloists orchestre les archets de paris'

print(textwrap.fill(row,width=55))
# saint george 1739 1799 violin concerti g 029 039 050
# symphonie concertante for two violins g 024 bertrand
# cervera in 024 039 christophe guiot in 024 029 and
# thibault vieux violin soloists orchestre les archets de
# paris
unutbu