Tuple MethodsTuple Methods

Let's dive into the primary methods associated with tuples.

  • len(t) - gives the number of elements in the tuple t;
  • t * n - produces n repetitions of the tuple t;
  • t.count(x) - counts how often x appears in the tuple t;

In the previous chapter, we highlighted that tuples are immutable. So, you might wonder: "How can I update a tuple with new values?" Unlike lists, you can't use methods like .append() or .extend() with tuples. However, remember a few chapters back when we discussed concatenation? That method works for tuples too:

  • tuple1 + tuple2 - this joins two tuples together (both elements must be tuples).

For instance, let's add some new data to the tuple we were working with in the last example.

Just a heads up: make sure the data you're adding is also in tuple format to successfully concatenate.

Everything was clear?

Section 4. Chapter 7