Reversing part of a string in Python -
let's have string stored in variable:
a = 'python'
now, a[2:4]
returns th
. how reverse part of string ht
returned instead?
this tried:
print a[2:4:-1]
but returned empty string. know can store result of a[2:4]
in new variable , reverse new variable. there way reverse part of string without creating new variable?
>>> = 'python' >>> a[2:4] 'th'
reverse substring using [::-1]
>>> a[2:4][::-1] 'ht'
or adjust indexes:
>>> a[3:1:-1] 'ht'
Comments
Post a Comment