Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKia Matthews
3,402 PointsWhy does self.[::-1] return the reverse of the string when used in defining the class?
I thought [::] was used for lists only?
1 Answer
Jon Mirow
9,864 PointsHi there! Slicing in python is a tool used for ordered collections. So strings, lists, tuples can all be sliced because they have an order. unordered collections like sets and dictionaries can't be sliced by index. What's unique about lists in python's ordered collections is that they can be changed without creating an new instance of the collection - they are said to be mutable. Strings and Tuples are immutable - you can slice them to get values from them, but if you wanted to change them in anyway you have to make a new string/tuple.
Hope it helps
Kia Matthews
3,402 PointsKia Matthews
3,402 PointsAh yes. I was thinking it was for mutable objects only. Thanks for clearing that up!