Join Tuples
What You'll Learn: In this tutorial, you'll discover how to join and multiply tuples in Python. Tuples are a way to store multiple items, and you can combine or repeat them easily!
Join Two Tuples
To join two or more tuples, you can use the + operator.
Example Code:
tuple1 = ("a", "b", "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
Output: ('a', 'b', 'c', 1, 2, 3)
What's Happening Here?
tuple1 + tuple2combines the items from both tuples intotuple3.
Multiply Tuples
If you want to multiply the content of a tuple a given number of times, you can use the * operator.
Example Code:
fruits = ("apple", "banana", "cherry")
mytuple = fruits * 2
print(mytuple)
Output: ('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')
What's Happening Here?
fruits * 2creates a new tuplemytuplewith the items offruitsrepeated twice.
Try It Yourself: Fun Exercises
- Combine Your Favorite Fruits:
- Create two tuples of different fruits.
- Use the
+operator to combine them into one tuple.
- Double Your Daily Routine:
- Write a tuple of activities you do in a day.
- Use the
*operator to repeat the tuple.
Summary:
In this Python tutorial, we learned how to join tuples using the + operator and multiply them using the * operator. These methods allow you to combine and repeat tuples easily. Keep experimenting and have fun with tuples in Python!