python
[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)
Output
10
20
30
============
10
20
============
60
70
============
c
e
g
i
k
20
30
============
10
20
============
60
70
============
c
e
g
i
k
Was this helpful?
Similar Posts
- Convert comma separated string values to tuple python
- Access items of tuple in Python
- Python Measure the execution time of small bits of Python code with the timeit module
- [Python] Use json.dumps() to pretty-print Python dicts
- [Python] The get() method on Python dicts and its "default" arg
- [Python] Different ways to test multiple flags at once in Python
- Python collection - List in python