1

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
gregoruar
  • 345
  • 3
  • 14
  • I added an answer I wanted to put here in https://stackoverflow.com/questions/3911483/python-slice-how-to-i-know-the-python-slice-but-how-can-i-use-built-in-slice-ob/57546434#57546434 about using numpy's `s_`, which I think may better answer your question. I was unable to post it as this question was so rapidly closed. – Roman Susi Aug 18 '19 at 16:34

1 Answers1

4

What you look for are the slice objects:

z = slice(18,26)
print(s[z])
GZ0
  • 4,055
  • 1
  • 10
  • 21