Python Don't understand this python function

  • Thread starter Thread starter zeion
  • Start date Start date
  • Tags Tags
    Function Python
AI Thread Summary
The discussion centers on understanding Python string slicing, specifically the expression s[1::-1] applied to the string s = 'hello'. It clarifies that s[::-1] reverses the string, while s[1::] slices from the second character to the end. The confusion arises with s[1::-1], which returns 'eh'. This is explained by breaking down the slicing parameters: it starts at index 1 (the letter 'e'), stops before index 0, and steps backwards, thus including 'e' and 'h' in reverse order. The thread also references Python's official documentation for further clarification on slicing behavior.
zeion
Messages
455
Reaction score
1

Homework Statement



Hi,

I'm not sure what this does:

s = 'hello'

I understand

s[::-1] would reverse the order of the letters and
s[1::] slices the first letter

but how come

s[1::-1] returns 'eh' ?


Homework Equations





The Attempt at a Solution

 
Technology news on Phys.org
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top