This question already has answers here:
Understanding slicing
(38 answers)
Closed 5 years ago.
s="Python"
print(s[0:6:-1])
print(s[::-1])
Output
'' # for first print function
'nohtyP' #for second print function
Both have the same meaning but gives different output, I don't understand this why?
from this answer to Understanding Python's slice notation:
If stride is negative, the ordering is changed a bit since we're counting down:
>>> seq[::-stride] # [seq[-1], seq[-1-stride], ..., seq[0] ]
>>> seq[high::-stride] # [seq[high], seq[high-stride], ..., seq[0] ]
>>> seq[:low:-stride] # [seq[-1], seq[-1-stride], ..., seq[low+1]]
>>> seq[high:low:-stride] # [seq[high], seq[high-stride], ..., seq[low+1]]
So you notice that when you specify high and/or low bounds when using negative stride, it's not possible to get all elements. You're always short of one.
So s[0:6:-1] prints nothing because low > high. But even with s[6:0:-1] you're short of one letter. The only argument which satisfies your requirements is None (or no argument): s[6:None:-1]
Related
This question already has answers here:
Understanding slicing
(38 answers)
Closed 7 months ago.
When i run this code it returns nothing
name = "Azura"
print(name[-1:-6])
Why does this happen ?
Because you're slicing everything in the string from index -1 to index -6. In the case of the string name, after converting negative index to positive, that is index 5 to index 0. In otherwords, when you do print(name[-1:-6]), you're doing print(name[5:0]), which obviously doesn't work, because python slices left to right. Therefore, if you want print(name[0:5]), just do print(name[-6:-1]).
TLDR: BAD print(name[-1:-6]) GOOD print(name[-6:-1])
If you want to use a minus index that's going from right to left you must always use a negative step
print(name[-1:-6:-1])
The direction of the slicing must be the same as the direction of the steps.
Also it's easier to use string[::-1] if you want to reverse a string.
This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
What is the length of d? d = [1, 2, 3][1:]. I can't understand what the [1:] mean at the end.
It’s referred to as ‘slicing’. Examples shown here.
From element 1 (the second element) through to the end, therefore a length of 2.
Aside: You’d (hopefully) never see this exact case in real-world practice, as it’s nonsense.
This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
Assuming the string has at least one character in it. If the string length is odd, then you may assume it returns (𝑛−1)/2 characters where n represents how many characters are in the original string.
For example:
'small' => 'sm'
I would also like to write another function that returns the 2nd half of a string.
You can just do an integer division (//) to get the integer value of the division to get the desired (n-1)/2 value for odd n. Therefore, having the following:
>>> my_string = "small"
>>> print(my_string[:len(my_string)//2])
sm
You could do the similar thing with using math.floor to be more explicit, but result is the same.
This question already has answers here:
How to explain the reverse of a sequence by slice notation a[::-1]
(7 answers)
Understanding slicing
(38 answers)
Closed 4 years ago.
I got this:
#slicing: [start:end:step]
s = 'I am not the Messiah'
#s[0::-1] = 'I'
So in this case
start=0, end=0, step=-1
Why is
s[0::-1] == 'I'
>>>> True
Because, -1 is a reversed stepping in this case.
Therefore when you say
s[0::-1]
You're actually going backward from position 0 to -1 where 0 is included
Therefore, returning I in your case.
Note that when I say position 0 to -1 I mean that it will include position 0 and stop slicing after since a -1 index is not valid (which is different from reversed indexing like my_list[-1])
Because your slice starts with index 0 and steps -1 at a time, which means it hits the boundary immediately, leaving just the first item in the sequence, i.e. 'I', in the slice.
This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
Please explain me what does this piece of code do.
h should be 32Byte result from sha256 calculation.
I am rewriting parts of this code for my project in C++ and I'm not sure if this switches byte order per 4byte chunk or change byte order on whole 32byte number.
def reverse_hash(h):
return struct.pack('>IIIIIIII', *struct.unpack('>IIIIIIII', h)[::-1])[::-1]
And, how does this array index work ?
[::-1]
Thanks for any and all info
[::-1] creates a new list with reversed order of elements
[::-1] reverse order of elements in a list, so this script change order of each 4-bytes subsequence (in order to change endiannes, I suppose):
>>> h = ''.join(map(str, range(0,21)))
>>> h
'01234567891011121314151617181920'
>>> struct.pack('>IIIIIIII', *struct.unpack('>IIIIIIII', h)[::-1])[::-1]
'32107654019821114131615181710291'
Equivalent expression:
>>> struct.pack('<IIIIIIII', *struct.unpack('>IIIIIIII', h))
'32107654019821114131615181710291'