[Tutor] string reversal using [::-1]

Alan Gauld alan.gauld at yahoo.co.uk
Mon Jun 12 13:54:37 EDT 2017


On 12/06/17 15:57, Vikas YADAV wrote:

> for i in range(len(s)-1, 1, -2):
>     print s[i]
> ---------------------------------------------
> 
> So my question is: how would you write "s[::-1]" in terms of a for loop for illustration purpose?

Exactly as above but replace -2 with -1

for i in range(len(s)-1, 1, -1):
    print s[i]

But that seems too obvious, am I missing something
subtle in your question?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list