Don't understand this python function

  • Context: Python 
  • Thread starter Thread starter zeion
  • Start date Start date
  • Tags Tags
    Function Python
Click For Summary
SUMMARY

The discussion centers on Python string slicing, specifically the expression s[1::-1] which returns 'eh' from the string s = 'hello'. The slicing syntax s[start:stop:step] is clarified, where start is 1, stop is omitted (defaulting to the start of the string), and step is -1, indicating a reverse order. This results in the characters at index 1 and 0 being included in the output.

PREREQUISITES
  • Understanding of Python string manipulation
  • Familiarity with Python slicing syntax
  • Basic knowledge of Python indexing
  • Experience with Python 2.3.5 or later versions
NEXT STEPS
  • Study Python string slicing in depth
  • Learn about Python string methods and their applications
  • Explore the differences between Python 2 and Python 3 string handling
  • Practice with various string slicing examples in Python
USEFUL FOR

Students learning Python, educators teaching string manipulation, and developers seeking to enhance their understanding of Python slicing techniques.

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

Similar threads

Replies
55
Views
7K
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K