How to iterate by sequence index in Python?

PythonServer Side ProgrammingProgramming

Sequence objects in Python are an ordered collection of items. Each item in the sequence (list, tuple and string) is accessible by index starting with 0.

To traverse elements in a list

>>> L1=[10,20,30,40,50]
>>> for i in range(len(L1)):
print (L1[i])

10
20
30
40
50

To slice one character at a time out of a string

>>> string ='TutorialsPoint'
>>> for i in range(len(string)):
print (string[i])

T
u
t
o
r
i
a
l
s
P
o
i
n
t
raja
Published on 15-Feb-2018 23:43:17
Advertisements