Search code snippets, questions, articles...

[Python] Slice of tuple

mytuple = (10,20,30,45,60,70)

for x in mytuple[0:3]:
    print(x)
    print(============)

# iterate through first 2 numbers
for x in mytuple[:2]:
print(x)
print(============)

# iterate through last (6-4)=2 numbers
for x in mytuple[4:]:
    print(x)
    print(============)

mystring= "abcdefghijkl"

# iterate through string symbols starting fron 2nd, skip every 2 symbols
for x in mystring[2::2]:
    print(x)
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place.
Output
10
20
30

============

10
20

============

60
70

============

c
e
g
i
k
Was this helpful?
0 Comments