【发布时间】:2015-08-30 10:19:36
【问题描述】:
我有这段代码,用于在 20x2 LCD 显示器上显示一些文本:
#!/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))
示例字符串将输出
This text will displ
ay on 2 LCD lines
我应该怎么做才能在不破坏单词的情况下拆分字符串?即使第二行变得更长并且不再显示,这也是如此。
我在javascript section 和ruby section 上阅读了一个类似的问题,但我无法将给定的答案翻译成我的 Python 案例。
【问题讨论】:
标签: python raspberry-pi