根据变量子集字符串

时间:2019-08-18 16:17:13

标签: python string indexing

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

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

1 个答案:

答案 0 :(得分:4)

您要寻找的是slice objects

z = slice(18,26)
print(s[z])