How to Take a Tuple as an Input in Python?
Last Updated :
19 Nov, 2024
Tuples are immutable data structures in Python making them ideal for storing fixed collections of items. In many situations, you may need to take a tuple as input from the user. Let's explore different methods to input tuples in Python.
The simplest way to take a tuple as input is by using the split() method to convert a single input string into individual elements and then converting that list into a tuple. Suitable for cases where you expect the user to input all elements in a single line.
Python
# Taking input
a = input("Enter elements separated by spaces: ").split()
# Converting list to tuple
t = tuple(a)
print(t)
Output:
Enter elements separated by spaces: 1 2 3 4 5
('1', '2', '3', '4', '5')
Explanation: The split() method splits the input string based on whitespace (by default), creating a list of elements. tuple(l) converts the list to a tuple.
We can also use map() in case of Homogeneous data types. This method allows you to take a single line of space-separated input, convert each element to a specified type (like integers), and store them in a tuple.
Python
# Taking multiple integer inputs from the user
t = tuple(map(int, input("Enter elements separated by spaces: ").split()))
print(t)
Let's take a look at other methods of taking tuple as input in python:
Using eval()
eval() method allows to directly input a tuple in standard tuple syntax, which eliminates the need of additional conversion.
Python
# Direct tuple input
t = eval(input("Enter a tuple: ")) # Ex: (1, 2, 3)
print(t)
Output:
Enter a tuple: (10, 20, 30)
(10, 20, 30)
Using a for Loop
A more flexible approach is to use a for loop to collect user input, which is useful when individual inputs need validation.
Python
# Taking input
n = int(input("Enter the number of elements: "))
a = []
for _ in range(n):
val = input(f"Enter element {_ + 1}: ")
a.append(val)
# Converting list to tuple
t = tuple(a)
print(t)
Output:
Enter the number of elements: 3
Enter element 1: 23
Enter element 2: 45
Enter element 3: 12
('23', '45', '12')
Explanation: It first prompts for the number of elements and uses a for loop to collect input one element at a time and at the end appends each input to a list which is then converted to a tuple.
List comprehension offers a concise way to collect inputs and convert them into a tuple. It combines input collection and conversion in a single line and provides a clean and readable solution for collecting inputs.
Python
# Taking input
n = int(input("Enter the number of elements: "))
t = tuple(input(f"Enter element : ") for i in range(n))
print(t)
Output:
Enter the number of elements: 4
Enter element : apple
Enter element : banana
Enter element : cherry
Enter element : date
('apple', 'banana', 'cherry', 'date')
Explanation: It first collects inputs using list comprehension in a single line and then converts the collected input list directly into a tuple.
Similar Reads
How to take integer input in Python? In this post, We will see how to take integer input in Python. As we know that Python's built-in input() function always returns a str(string) class object. So for taking integer input we have to type cast those inputs into integers by using Python built-in int() function.Let us see the examples:Exa
3 min read
How toGet First and Last Elements from a Tuple in Python Tuples are immutable sequences in Python that can store a collection of items. Often, we might need to the retrieve the first and last elements from the tuple. In this article, we'll explore how to achieve this using the various methods in Python. Get First and Last Elements from a Tuple Using Index
2 min read
Convert Tuple to List in Python In Python, tuples and lists are commonly used data structures, but they have different properties:Tuples are immutable: their elements cannot be changed after creation.Lists are mutable: they support adding, removing, or changing elements.Sometimes, you may need to convert a tuple to a list for furt
2 min read
Get a list as input from user in Python We often encounter a situation when we need to take a number/string as input from the user. In this article, we will see how to take a list as input from the user using Python.Get list as input Using split() MethodThe input() function can be combined with split() to accept multiple elements in a sin
3 min read
Tuple as function arguments in Python Tuples have many applications in all the domains of Python programming. They are immutable and hence are important containers to ensure read-only access, or keeping elements persistent for more time. Usually, they can be used to pass to functions and can have different kinds of behavior. Different c
2 min read