Testing testing… one two..
I’m learning python. Well, in reality I’ve been learning python for quite some time now. Since I usually live in C by day I can only use my free time to try out python.
It started with a desperate need for knowing how to make programs for the raspberry pi. Then, as time went on I have been more used to the syntax. Fortunately everything I do has been tried before so I can look it up on the net.
Anyway, this is just a real quick one. If you ever find yourself having to split a sentence into smaller chunks of text but preserving the readability of the string you really want to split on the spaces. How to do that?
s = "This is a test that should be long enough to break into multipls lines.. "
w = s.split() #split string into words
sentence=[""] #list to hold split sentences
lineP=0 #line 'pointer': which line should the word be added to
for i in range(len(w)): #for all words in the string
if ((len(sentence[lineP])+len(w[i])+1)>maxlength): #if the current line is too long
sentence.append("") #add a new line
lineP=lineP+1 #and point to that
sentence[lineP]=sentence[lineP]+w[i]+" " #add the word and a space
for i in range(lineP+1): #show the result
Not really that advanced after all!