Lessons

Python Basics

Python Variables

Operators in Python

Conditional Statements in Python

Python Lists

Python Tuples

Python Sets

Python Dictionaries

Loops in Python

Python Arrays and Functions

Conclusion

Join Tuples in Python

Join Tuples in Python

In Python, tuples are immutable collections that can be easily joined or repeated. This lesson will cover how to join two or more tuples using the + operator and how to multiply the contents of a tuple using the * operator.

1. Joining Two Tuples

To join or concatenate two tuples, you can use the + operator. This creates a new tuple that combines the elements of both tuples.

Example: Join Two Tuples

python
1
2
3
4
5
6
7
8
9
10
# Defining the first tuple
tuple1 = ("a", "b", "c")

# Defining the second tuple
tuple2 = (1, 2, 3)

# Joining the two tuples
tuple3 = tuple1 + tuple2

print(tuple3)  # Output: ('a', 'b', 'c', 1, 2, 3)

2. Multiplying Tuples

If you want to repeat the contents of a tuple multiple times, you can use the * operator. This operator allows you to multiply the tuple's contents by a specified integer.

Example: Multiply the Fruits Tuple by 2

python
1
2
3
4
5
6
7
# Defining the fruits tuple
fruits = ("apple", "banana", "cherry")

# Multiplying the tuple
mytuple = fruits * 2

print(mytuple)  # Output: ('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')

Frequently Asked Questions