本文介绍了根据变量子集字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Suppose we have a long string, for example:

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."

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

Now, We all know how to extract from this string letters based on indexes:

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

但是有什么方法可以将索引分配给一个变量,然后基于该变量对列表进行子集化?它看起来应该像这样:

But is there any way, how I can assign this indexes to a variable, and then subset the list based on this variable? It should look something like that:

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

推荐答案

您要查找的是切片对象:

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

这篇关于根据变量子集字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 16:46