基于变量的字符串子集

2024-06-01 00:59:01 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我们有一个长字符串,例如:

s = "The long-string instrument is a musical instrument in which the string is of such a length that the fundamental transverse wave is below what a person can hear as a tone."

现在,我们都知道如何根据索引从字符串中提取字母:

z = s[18:26]
print(z)
strument

但是有没有办法,我如何将这个索引分配给一个变量,然后根据这个变量对列表进行子集划分?应该是这样的:

z = [18:26]
print(s.z)
strument

Tags: ofthe字符串inwhichstringislength