Why cant i negative slice my value? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why cant i negative slice my value?

Guys im trying to negative slicing the string "Hello" but the output is just a blank space like nothing at all. But if i do the normal slicing it will just work fine. How? HELP!! This worked a = "Hello" print(a[0:3]) output -> Hel This didnt! a = "Hello" print(a[-1:-3]) output -> nothing i want it to print ll how? Help!!

16th Jun 2020, 7:51 PM
slawbear
slawbear - avatar
5 Answers
+ 2
No output because end (-3) comes before start (-1). If you want output you can: - swap start and end (a[-3:-1]) - instruct the interpreter to go backwards (a[-1:-3:-1])
16th Jun 2020, 8:05 PM
Bilbo Baggins
Bilbo Baggins - avatar
16th Jun 2020, 8:43 PM
HonFu
HonFu - avatar
+ 1
print(a[-1::-3]) there are tow colon(:)
16th Jun 2020, 7:54 PM
Muhammad Galhoum
Muhammad Galhoum - avatar
+ 1
why do you need to put two colon?
16th Jun 2020, 7:55 PM
slawbear
slawbear - avatar
+ 1
Every number express about indexes First number : determind which number starting from it Second number Explains how many will pass to the last number and the default is 1 Last number : The last number is not printed, but the number that precedes or is subtracted from index number 1 so that we know we will print to any number
16th Jun 2020, 8:07 PM
Muhammad Galhoum
Muhammad Galhoum - avatar