For example,
str = "hello"
str[1::3]
And where can I find this in Python documentation?
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.
Artturi Jalli. In Python, [::-1] means reversing a string, list, or any iterable with an ordering. For example: hello = "Hello world"
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.
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.
in sequences' description:
s[i:j:k] slice of s from i to j with step k
The slice of
s
fromi
toj
with stepk
is defined as the sequence of items with indexx = i + n*k
such that0 <= n < (j-i)/k
. In other words, the indices arei
,i+k
,i+2*k
,i+3*k
and so on, stopping whenj
is reached (but never includingj
). Ifi
orj
is greater thanlen(s)
, uselen(s
). Ifi
orj
are omitted orNone
, they become “end” values (which end depends on the sign ofk
). Note,k
cannot be zero. Ifk
isNone
, it is treated like 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With