我有点像蟒蛇!我正在尝试将一个字符串(长度在0到32个字符之间)分割成两个16个字符的块,并将每个块保存为一个单独的变量,但我想不出怎么做。
这是一个伪代码大纲,我的意思是:
text = "The weather is nice today"
split 'text' into two 16-character blocks, 'text1' and 'text2'
print(text1)
print(text2)这将产生以下结果:
The weather is n
ice today 我把输入的文字显示在2x16字符的液晶显示器上,连接到一个覆盆子圆周率,我需要把文本分割成行来写到液晶显示器上--我把文字写到液晶显示器上,就像这样:lcd.message(text1 "\n" text2),所以块必须长16个字符。
发布于 2018-07-31 15:45:22
可以通过指定索引将文本分割成两个字符串变量。16基本上是0到15,16 : 16是字符串中的最后一个字符。
text1 = text[:16]
text2 = text[16:]发布于 2018-07-31 15:48:03
text = "The weather is nice today"
text1, text2 = [text[i: i + 16] for i in range(0, len(text), 16)]
print(text1)
print(text2)它将印刷:
The weather is n
ice today发布于 2018-07-31 15:48:51
这将适用于任何text
text = "The weather is nice today"
splitted = [text[i:i+16] for i in range(0, len(text), 16)]
print (splitted) # Will print all splitted elements together或者你也可以像
text = "The weather is nice today"
for i in range(0, len(text), 16):
print (text[i:i+16])https://stackoverflow.com/questions/51617221
复制相似问题