How to take tuple input in Python
In this post, we are going to learn how to take tuple input in python. We will take input from the user by using the input() function. and split it by using the split() function and tuple() to convert it into a tuple.
How to take tuple input in Python
To read a string input from the user in Python, the built-in input() function is used. The input() function always takes input as a string. The input() method takes a prompt as a parameter. A prompt is a string message that is displayed before input and control are given to the user.
- In this Python program, We have used the input() function to prompt a message to take string input separated by space.
- Split() : The split() method to split string by space ,we can use any whitespace space,comma(,),colon(:) as per need.
- The tuple() to make a tuple from the input.
#python program to take tuple input in python values = input('Please enter some values:') #spliting the input values by space input_tuple = tuple(int(val) for val in values.split()) print('tuple:',input_tuple)
Please enter some values:3 6 9 12 13 111 tuple: (3, 6, 9, 12, 13, 111)
2. How to take tuple input in python
In this Python program, We have used the input() function to prompt a message and take string input separated by comma(,). The Split() method to split string by comma(,). The tuple() to make a tuple from the input. Let us understand how to create a tuple from input in python.
#python program to take tuple input in python input_tuple = tuple([eval(val) for val in input("Please enter some values: ").split(',')]) print('tuple:',input_tuple)
Please enter some values:3,6,9,12,15 tuple: ('3', '6', '9', '12', '15')
4. Take input range of tuple elements in Python
In this Python program, First, we have asked users to input the range of elements in the tuple. The for loop to iterate over the range and on each iteration ask the user to enter the element and list. append() to append item into the list. The tuple() to convert the final list to a tuple.
mylst = [] num = int(input("Enter number of elements : ")) for n in range(0, num): element = (input("Please enter a num:")) mylst.append(element) mytuple =tuple(mylst) print(mytuple)
Enter number of elements : 5 Please enter a num:12 Please enter a num:13 Please enter a num:14 Please enter a num:15 Please enter a num:16 ('12', '13', '14', '15', '16')
5. Take a list of tuple input in Python
To make a list of tuples from the user, Initially, we have asked the user to input the elements separated by space by using the split() method and separated each list by a newline character(\n), and appended each tuple into the list by using the list.append(). Let us understand with examples.
result_lst = [] line = input("Please enter value to create a list of tuples:\n") while(line != ''): result_lst.append(tuple(line.split())) line = input() print(result_lst)
Please enter value to create a list of tuples: 12 13 14 15 16 17 18 19 20 21 22 23 a b c d e f g h i [('12', '13', '14', '15', '16', '17'), ('18', '19', '20', '21', '22', '23'), ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i')]
Summary
In this post, we have learned how to take tuple input in Python with examples.