This question already has answers here:
Understanding slicing
(38 answers)
Closed 8 years ago.
I keep seeing this: s[::-1] in Python and I don't know what it does. Sorry if this is a question but I'm new to python and generally programming.
It reverses a sequence using slicing.
>>> s = 'hello'
>>> s[::-1]
'olleh'
The slice notation [] is a way to get a subset of some iterable container. It has the syntax
[start : stop : step]
See this post for more details.
Slicing is an operation that gives you some elements out of a sequence.
s[a:b:c] means "the items starting at a, stopping at b, with a step of c".
If you have s[::-1] that means "the whole sequence, going backwards".
Related
This question already has answers here:
What is the return value of the range() function in python?
(5 answers)
Closed 6 months ago.
On an online course, the teacher says that range() produces a tuple, adding that this is the reason why we have to convert it into a list (thanks to the list() function) if we want to modify it.
Is this statement true ?
Because in the official documentation, they dont talk about tuple at all in the range() section : https://docs.python.org/3/tutorial/controlflow.html#the-range-function
Starting with python3, range returns a range object which is a sequence type which, among other things, can be iterated to create a list out of it.
Relevant docs about the range type.
Before that, it used to return a list, like Klaus commented. (docs)
This question already has answers here:
Understanding slicing
(38 answers)
Closed 1 year ago.
First time using StackOverflow, apologies in-case of any issues.
In python,
list[::-1] returns the reversed list
list[0:len(list)+1] returns the complete list
So why list[0:len(list)+1:-1] returns an empty list?
Further, for a list l= [0,1,2,3,4,5], if I want like [4,3,2]:
Trying l[2:5:-1], returns an empty list. But l[2:5][::-1] works.
Can anyone explain why this is happening? Or what is python actually doing when we slice a list, with a value for step?
Thanks in advance :)
some_list[start:stop:step] means give me elements starting at start and incrementing by step until you hit stop.
Well, if you start at 0 and increment by -1, you never actually reach the stop.
This question already has answers here:
Understanding slicing
(38 answers)
Closed 5 years ago.
I am very new to Python and I encountered one issue that I cannot understand. Let say I have string variable:
myVar = "abcdefgh"
I want to display it backward, no problem:
print(myVar[::-1])
and I get hgfedcba. Nothing surprising here. I should get the same with this somewhat verbose code:
print(myVar[len(myVar)-1:0:-1])
but this time the result is hgfedcb. Then I have tried not to subtract 1 from len(myVar) and the result was exactly the same. I do not understand why, especially that lines:
print(myVar[::1])
print(myVar[0:len(myVar):1])
display the same results.
So, my question is why print(myVar[len(myVar):0:-1]) does not display "a"?
The verbose equivalent of print(myVar[::-1]) would be:
print(myVar[-1:-1-len(myVar):-1])
# Or
# print(myVar[len(myVar)-1:-1-len(myVar):-1])
# but this makes the the length invariant less obvious
Note that the stop parameter is exclusive, and in order to get to the actual -1, you have to additionally subtract the full length as negative indexing starts at the end of the sequence. Note also how (stop-start)*step is still the length of the slice.
This question already has answers here:
Why is this list slice treating 0 and 1 as the same in Python? [duplicate]
(2 answers)
Understanding slicing
(38 answers)
Closed 8 years ago.
Let me preface this by saying I'm new to Python, come from Ruby, and I don't have much specific knowledge about how Python works.
For one of my current projects, I'm creating a new feature in a computational chemistry Django application that reads in PDBs and then does calculations on them. After adding my code, I was getting an error that Python can't typecast a string as a float, and looked at the library that parses the PDBs.
I was quickly confused by how Python's slice notation works. For example:
str = 'Hello this is Josh'
str[0:2] #=> 'He'
str[2] #=> 'l'
What I thought calling str[0:2] would result it would be Hel, not He, since index 0 to 2 is 3 big.
Is there a reason that this happens this way, and why str[m:n] gives from m to n-1, not from m to n?
It's so that:
str[0:2] + str[2:4] == str[0:4]
And
str[0:len(str)] == str
In general, it's conventional for sets of numbers to be defined this way; inclusive of the first listed number, exclusive of the second.
Esdgar Dijkstra wrote up a fairly well known argument for both this convention, and the convention of starting array indices at 0.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
reverse a string in Python
I tried searching the python documentation for a certain slicing exapmle
lets say I have this string
a = "ABCD"
when I write:
a[::-1]
I get the reversed string
"DCBA"
I can't understand how exectaly it works. None of the examples I saw were with two colons. what does it mean? how does it work?
thank you!
The full syntax of a slicing is
a[start:stop:step]
The step value denotes by how much to increase the index when going from one element of the slice to the next one. A value of -1 consequently means "go backwards". If start and stop are omitted as in your example, the default is to use the whole string (or more generally, the whole sequence, as this also works for lists and tuples).