-2

I used textwrap.fill (textwrap.fill(text, 6)) to limit each line in only 6 characters, but there is a problem with using this command because my purpose is go to new line exact at 6 character, I mean:

for example using textwrap.fill(I am a student, 8):

what I want:

   (I am a s
          tudent)

output:

(I am a
    student)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321

1 Answers1

0

One approach:

>>> text = 'I am a student, 8'
>>> text = 'I am a student'
>>> for i in range(0, len(text), 8):
...     print text[i:i+8]
...
I am a s
tudent

for i in range(0, len(text), 8) means "Give me numbers starting at 0, incrementing by 8 at a time, and ending before the length of the text."

EDIT

If you want the value in a single string:

>>> wrapped = "\n".join(text[i:i+8] for i in range(0, len(text), 8))
>>> wrapped
'I am a s\ntudent'
user94559
  • 59,196
  • 6
  • 103
  • 103