Know That Tuples and Strings Are Iterable
00:00
And finally, another similarity between tuples and strings is that tuples are iterable, which means you can loop over them. Try that out. So I can keep working with the numbers
tuple here, and I can say for each number
in numbers
,
00:20
print(number)
, and you can see that it’s iterable and Python can access each element of the tuple separately and then prints it out—first 1
, then 2
, and then 3
.
00:35
Same with a string. For character
in word
, I can print out the character
, and then Python prints out each separate character of the string "Python"
.
00:51 There’s another thing where tuples and strings are similar, which is that they’re iterable, just like any collection in Python. And this concludes our little excursion about the similarities between tuples and strings.
01:05 In our next lesson, we’re going to look at tuple unpacking, and I’ll show you what you can do with that.
Become a Member to join the conversation.