Why is that slicing expression generating that output [duplicate] - python

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.

Related

Reversing a string including the first letter in python [duplicate]

This question already has answers here:
Why does reversing a list using slice notation with 0 as "stop" not return the entire list?
(9 answers)
How do I reverse a string in Python?
(19 answers)
Closed 10 days ago.
I am trying to reverse a string in python but i cannot include the first letter.
I tried this code:
a = "Helloworld"
print(a[3:0:-1])
but it doesn't work.
I also tried:
a = "Helloworld"
print(a[3:-1:-1])
It displays nothing when i try this.
The code you tried doesn't work because the slice a[3:0:-1] starts at index 3 and goes all the way to index 0 (in reverse), but it includes index 0, which is the first letter of the string.
The slice a[3:-1:-1] starts at index 3 and goes to the index before the last one (-1), but in reverse. This would give an empty string because the step value of -1 goes in the opposite direction of the start and end indices.
To reverse the string excluding the first letter, you can slice it like this:
a = "Helloworld"
print(a[1:][::-1])

Slicing a String gives no output in python [duplicate]

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.

How do I subtract odd index values and add even index values in a list? (a-b+c-d+e) [duplicate]

This question already has answers here:
Writing a function that alternates plus and minus signs between list indices
(7 answers)
Closed 2 years ago.
Given a list of integers, I want to subtract all the integers with an odd index and add all the integers with an even index. Is there any compressed way to do this without the regular "while loop with iterator and add/subtract"?
Use slicing with a step:
n = sum(L[0::2]) - sum(L[1::2])
The sequence slicing syntax L[i:j:k] is documented here, specifically refer to the note 5.

Why string reversing is not happening? [duplicate]

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]

Is there a Python idiom for the last n elements of a sequence? [duplicate]

This question already has answers here:
How to get last items of a list in Python?
(5 answers)
Python - How to extract the last x elements from a list [duplicate]
(4 answers)
Closed 9 years ago.
e.g., for a sequence of unknown length, what is the most "Pythonic" way of getting the last n elements?
Obviously I could calculate the starting and ending indices. Is there anything slicker?
Yes, by using negative indices:
last_five = somesequence[-5:]
Negative indices in a slice are relative to the sequence length.
Use negative indexing.
seq[-1] is the last element of a sequence. seq[-3:] gives you the last three.
Try:
sequence[-n:]
(text to make Stack Overflow happy)

Categories