将字符串变量拆分为 16 个字符长的块
Splitting a string variable into chunks 16 characters long
我有点python菜鸟!我试图将一个字符串(长度在 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
我正在显示在连接到 raspberry pi 的 2x16 字符 LCD 上输入的文本,我需要将文本分成几行以写入 LCD - 我正在将文本写入 LCD,例如这:lcd.message(text1 "\n" text2)
所以块的长度必须正好是 16 个字符。
text = "The weather is nice today"
text1, text2 = text[:16], text[16:32]
print(text1)
print(text2)
打印:
The weather is n
ice today
可以通过指定索引将文本分成两个字符串变量。 [:16] 基本上是 0 到 15,[16:] 是 16 到字符串中的最后一个字符
text1 = text[:16]
text2 = text[16:]
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
尝试这样的事情:
s = "this is a simple string that is long"
size = 8
for sl in range(0, int(len(s)/size)):
out = s[sl:sl + size]
print(out, len(out))
一个字符串就像一个列表。
您可以拆分它或计算字符串中的字符数。
示例:
word = 'www.BellezaCulichi.com'
total_characters = len(word)
print(str(total_characters))
# it show 22, 22 characters has word
您可以拆分字符串并获取前 5 个字符
print(word[0:5])
# it shows: www.B
# the first 5 characters in the string
print(word[0:15])
# split the string and get the 15 first characters
# it shows: www.BellezaCuli
# the first 5 characters in the string
您可以将拆分结果存储在变量中:
first_part = word[0:15]
这适用于任何 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])
我有点python菜鸟!我试图将一个字符串(长度在 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
我正在显示在连接到 raspberry pi 的 2x16 字符 LCD 上输入的文本,我需要将文本分成几行以写入 LCD - 我正在将文本写入 LCD,例如这:lcd.message(text1 "\n" text2)
所以块的长度必须正好是 16 个字符。
text = "The weather is nice today"
text1, text2 = text[:16], text[16:32]
print(text1)
print(text2)
打印:
The weather is n
ice today
可以通过指定索引将文本分成两个字符串变量。 [:16] 基本上是 0 到 15,[16:] 是 16 到字符串中的最后一个字符
text1 = text[:16]
text2 = text[16:]
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
尝试这样的事情:
s = "this is a simple string that is long"
size = 8
for sl in range(0, int(len(s)/size)):
out = s[sl:sl + size]
print(out, len(out))
一个字符串就像一个列表。
您可以拆分它或计算字符串中的字符数。
示例:
word = 'www.BellezaCulichi.com'
total_characters = len(word)
print(str(total_characters))
# it show 22, 22 characters has word
您可以拆分字符串并获取前 5 个字符
print(word[0:5])
# it shows: www.B
# the first 5 characters in the string
print(word[0:15])
# split the string and get the 15 first characters
# it shows: www.BellezaCuli
# the first 5 characters in the string
您可以将拆分结果存储在变量中:
first_part = word[0:15]
这适用于任何 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])