r/pythonhelp • • Jan 28 '22

a very noob question

i wrote these two lines:

>>> numbers = "123456789"

>>> numbers[-9:-1]

than it said '12345678' instead of '123456789'

what to do?

3 Upvotes

5 comments sorted by

1

u/Goobyalus Jan 28 '22

When you slice [a:b], the result will include indices a through b-1.

1

u/Goobyalus Jan 28 '22

There is a pretty in depth answer here if you have any other questions about slicing

https://stackoverflow.com/questions/509211/understanding-slice-notation

0

u/ragnarok628 Jan 28 '22

By setting the 'start' and 'stop' sections, you just sliced it, you didn't reverse it. You want the 'step' section to be -1, you can leave the start and stop sections alone. So:

>>>numbers[::-1] 

Should do what you want

1

u/Goobyalus Jan 28 '22

I don't think OP was trying to reverse it, they were wondering why the 9 was missing.