DEV Community

PERUMAL S
PERUMAL S

Posted on

Python Day 7

learning

index

always use character inside of this [] box
message =("hello world")
index number start from (0,1,2,3......)

h e l l o w o r l d
0 1 2 3 4 5 6 7 8 9 (positive index)
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 (negative index)

it will possible to find out this position

code 1
message ="hello world"
message [5]
result: o
code 2
message [-1]
result: d

slicing

h e l l o w o r l d
0 1 2 3 4 5 6 7 8 9 (positive index)
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 (negative index)
start ----------------------------end (stop) (forward direction)
(stop)end----------------------------start (reverse direction)

we can spectate the words based on the character position

[0:9] #[start:stop:(step or direction)]

slicing possible forward direction and reverse direction

NOTE
while reverse direction must to be use step -1
e.g
[-1:-10;-1] result reverse of "dlrowolleh"
[9:1:-1] result reverse of "dlrowolleh

ref

Image description

Top comments (0)