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).
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:
Are there limits to using string.lstrip() in python? [duplicate]
(3 answers)
Closed 8 years ago.
So I have a super long string composed of integers and I am trying to extract and remove the first three numbers in the string, and I have been using the lstrip method (the idea is kinda like pop) but sometimes it would remove more than three.
x="49008410..."
x.lstrip(x[0:3])
"8410..."
I was hoping it would just remove 490 and return 08410 but it's being stubborn -_- .
Also I am running Python 2.7 on Windows... And don't ask why the integers are strings. If that bothers you, just replace them with letters. Same thing! LOL
Instead of remove the first 3 numbers, get all numbers behind the third position. You can do it using : operator.
x="49008410..."
x[3:]
>> "8410..."
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".