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?
Asked
Active
Viewed 127 times
-1
-
google "python wrap string" (before you ask) – jonatan Oct 31 '17 at 22:27
1 Answers
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