s='12345'. s[::-2]='531', s[:1:-2]='53',s[:4:-2] or s[:-1:-2] or s[1:4:-2]=' '. How do we get these outputs | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

s='12345'. s[::-2]='531', s[:1:-2]='53',s[:4:-2] or s[:-1:-2] or s[1:4:-2]=' '. How do we get these outputs

5th Dec 2017, 5:39 PM
Ajeya Bhat
Ajeya Bhat - avatar
1 Answer
+ 1
print( s[a:b:+-c]) it works like ( for ( k= a; k<b; k= k+c ){ print(s[k]) } # using C for - loop or for negative step (decrement) for (k=a; k>b ; k = k - c){} so s[:-1:-2] is same as s[-1:-1:-2] , or s[4:4:-2] note the index -1 is the last element in py-list.
5th Dec 2017, 6:58 PM
yuri