Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of the two colons in this Python string-slicing statement?

Tags:

python

slice

For example,

str = "hello"
str[1::3]

And where can I find this in Python documentation?

like image 678
clark Avatar asked Jun 18 '09 15:06

clark


People also ask

What is the purpose of colon in Python?

In Python, a colon is required at the beginning of every block of code. It is easier to explain with an example. Notice how at the end of the if statement I have a colon. This tells Python that the next line of indented code should only be run IF the condition is true.

What does :: means in Python?

Artturi Jalli. In Python, [::-1] means reversing a string, list, or any iterable with an ordering. For example: hello = "Hello world"

What does 2 colons mean?

The double colon ( :: ) may refer to: an analogy symbolism operator, in logic and mathematics. a notation for equality of ratios. a scope resolution operator, in computer programming languages.

What is :: In slicing?

Consider a python list, In-order to access a range of elements in a list, you need to slice a list. One way to do this is to use the simple slicing operator i.e. colon(:) With this operator, one can specify where to start the slicing, where to end, and specify the step.


1 Answers

in sequences' description:

s[i:j:k]    slice of s from i to j with step k

The slice of s from i to j with step k is defined as the sequence of items with index x = i + n*k such that 0 <= n < (j-i)/k. In other words, the indices are i, i+k, i+2*k, i+3*k and so on, stopping when j is reached (but never including j). If i or j is greater than len(s), use len(s). If i or j are omitted or None, they become “end” values (which end depends on the sign of k). Note, k cannot be zero. If k is None, it is treated like 1.

like image 140
SilentGhost Avatar answered Oct 26 '22 06:10

SilentGhost