Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I save a range to a variable?

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

For example

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

I wish to return bc. However, the second line does not work.

like image 588
macabeus Avatar asked Oct 28 '15 04:10

macabeus


People also ask

Can you set a range as a variable?

Like any other variable, we can give any name to the variable, but the data type we assign to them should be a “Range.” Once the data type is assigned to the variable, it becomes an “Object Variable,” and unlike another variable, we cannot start using the variable before we set the reference of objects in case of ...

How do you assign a range to a variable in Excel?

Use the “Dim” keyword and enter the name of the variable. Declare keyword as Range. Type the name of the variable. Specify the range that you want to assign to the variable.

Can a range in VBA be a variable?

Now let us use Range as a variable in VBA, we can use range as variable and set the variable with the values using the range property method. In this example, we will discuss how to use the range variable to fetch data from a cell and display it on the screen.


1 Answers

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

>>> my_string = "abcdef"
>>> my_interval = slice(1, 3)
>>> print(my_string[my_interval])
like image 150
Abhijit Avatar answered Sep 19 '22 08:09

Abhijit