I have this code which is meant to display some text on a 20x2 LCD display:
#!/usr/bin/python
LCDCHARS = 20
LCDLINES = 2
def WriteLCD(text_per_LCD):
chunked = (text_per_LCD[i:LCDCHARS+i] for i in range (0, len(text_per_LCD), LCDCHARS))
count_l = 0
for text_per_line in chunked:
# print will be replaced by actual LCD call
print (text_per_line)
count_l += 1
if count_l >= LCDLINES:
# agree to lose any extra lines
break
WriteLCD("This text will display on %s LCD lines" % (LCDLINES))
The example string will output
This text will displ
ay on 2 LCD lines
What should I do to split the string without breaking the words? This even if the second line becomes longer and goes out of display.
I read a similar question on javascript section and another one in ruby section, but I was not able to translate the given answers into my Python case.