Why cant i negative slice my value? | Sololearn: Learn to code for FREE!

+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!!

6/16/2020 7:51:41 PM

cloroxide

6 Answers

New Answer

+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])

+2

cloroxide p="abcdefghi" print(p[::3]) Output : adg SYNTAX : string [start : stop : step] if start has nothing , default 0 is taken Ex : string [:7:2] if stop has nothing , default length of string is taken Ex : string [2::2] if step has nothing , default 1 is taken Ex : string [4:7:]

+2

Take a look here, it's all explained.https://www.sololearn.com/learn/Python/2453/?ref=app

+1

print(a[-1::-3]) there are tow colon(:)

+1

why do you need to put two colon?

+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