Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Common Tuple Methods in Python | Other Data Types in Python
Introduction to Python

book
Common Tuple Methods in Python

Tuples in Python come with a few built-in operations that allow you to retrieve information and manipulate them indirectly. Here are some key methods:

  • len(t): returns the number of elements in tuple t;
  • t * n: creates n repetitions of tuple t;
  • t.count(x): counts how often x appears in t.

Since tuples are immutable, you might wonder: How do I add or modify elements in a tuple? Unlike lists, tuples do not support methods like .append() or .extend() for adding elements directly. However, there is a workaround called tuple concatenation.

  • tuple1 + tuple2: merges two tuples (both must be tuples).
# Initial tuple
US_Info = ("USA", 9629091, 331002651)

# New data
US_Info_new = ("Washington D.C.", 50)

# Concatenate two tuples and save the result
US_Info_upd = US_Info + US_Info_new
print(US_Info_upd)
123456789
# Initial tuple US_Info = ("USA", 9629091, 331002651) # New data US_Info_new = ("Washington D.C.", 50) # Concatenate two tuples and save the result US_Info_upd = US_Info + US_Info_new print(US_Info_upd)
copy
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 7
some-alt