Can I save the interval in a variable?

Is there a way to assign an interval to a variable?

for example

my_string = "abcdef" my_interval = 1:3 print(my_string[my_interval]) 

I want to come back bc . However, the second line does not work.

+7
variables python
source share
1 answer

You need to use the python built-in slice() function to assign the slice variable. Your current syntax is not a valid python syntax.

 >>> my_string = "abcdef" >>> my_interval = slice(1, 3) >>> print(my_string[my_interval]) 
+6
source share

All Articles