It is necessary to make one cut of the line, which would remove the 1st and last character in the line, without using len() .
|
2 answers
The values ​​passed to the slices can be negative, in which case the count will be taken from the end of the line.
To get a string without the first and last element, the slice should look like this: [1:-1]
print("Hello World!"[1:-1]) # ello World - @MaxU, and there brackets have become mandatory? - Grundy
- In Python 3.x, the “print” statement was removed and replaced with the “print ()” function. The feature variant will work in both Python 2.x and Python 3.x - MaxU
- @MaxU, aah, I see :-) - Grundy
- one@MaxU, the link to the example also changed, now 3.6 - Grundy
|
The slice can still be described as an object via slice :
sl = slice(1, -1) print("Hello World!"[sl]) # "ello World" print("abc123"[sl]) # bc12 |