-1

i am using python 3. I need to be able to wrap a string every 30 characters. ([30 characters] \n [rest of characters]. Is there any way I can do this?

Benster T
  • 37
  • 1
  • 3

1 Answers1

0

assuming input contains your string you could do it like this:

wrap = 30
myList = [input[i:i+wrap] for i in range(0, len(input), wrap)]
print myList

@jonatan is right btw, a quick google search would have brought you, among other answers to this answer from a similar question a view years ago.

b__r
  • 110
  • 2
  • 7
  • Thanks, a lot! Could you please talk me through what the code does, it almost works exactly as I need but there is ['input'] around the output. – Benster T Oct 31 '17 at 22:55
  • I'm not exactly sure what you mean with "there is ['input'] around the output." but the above code splits your input in a list where each element is 30 characters long. I edited my answer to make it more clear. – b__r Oct 31 '17 at 23:08