I just started learning Python, and I was just getting into strings. So I have 3 commands here
_teststr = "IneedToFocusOnMyFuture"
print('1.',_teststr[::-1])
print('2.',_teststr[2:12:2])
print('3.',_teststr[2:12:-1])
Output:
1. erutuFyMnOsucoFoTdeenI
2. edoou
3.
Why the 3rd output is blank instead of having 'uoode'
? What is going under the hood? Could anyone please explain?
CodePudding user response:
if you want to reverse the specific string.
print('3.',_teststr[2:12][::-1])
Lets understand the syntax
str[start:end:step]
start denotes from where to splice and end denotes where to end the splice.
The step parameter denotes the number of elements to leave after a successful splicing.By default step is 1.
Example:
str="naveen"
str[1:3] //av
#with including step
str[1:3:1] //av
str[1:4:2] //ae
step defines the number of elements to leave after a consideration.So it cant be negative.