python - Split Sentance Into Even Lines (With full words) -


this question has answer here:

i using 16 x 2 character lcd. using show recent tweets.

i need method returns list of strings display on display.

for example input = "lorem ipsum dolor sit amet, consectetur adipiscing elit. donec aliquet lorem ac eros lobortis laoreet."

it takes strings , splits list of strings 16 characters each. however, these strings must complete words not split word in two.

so expected output ["lorem ipsum", "dolor sit amet,", "consectetur", "adipiscing elit.", "donec aliquet", "lorem ac eros", "lobortis", "laoreet."] each string not split words , strings no more 16 charcaters long.

i can display on lcd fine need method can return list of strings meet criteria above

i have seen others words need characters

i believe it:

def split16(str_in):     words = str_in.split()     y = [words[0] + ' ']     index = 0     word in words[1:]:         if len(word) + len(y[index]) < 16:             y[index] += word + ' '         else:             index += 1             y.append(word + ' ')     return y 

warning assumes no words more 16 characters, safe assume since said don't want words go on more 1 line.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -