Creating a tuple from user Input in Python

avatar

Borislav Hadzhiev

Last updated: Aug 24, 2022

banner

Photo from Unsplash

Creating a tuple from user Input in Python #

To create a tuple from user input:

  1. Use the input() function to take input from the user.
  2. Use the str.split() function to split the input string into a list.
  3. Use the tuple() class to convert the list to a tuple.
main.py
import ast # ✅ create a tuple from string user input my_tuple = tuple(input('Enter space-separated words: ').split()) print(my_tuple) # ------------------------------- # ✅ create a tuple from integer user input user_input = input('Enter space-separated integers: ') my_tuple = tuple(int(item) for item in user_input.split()) print(my_tuple) # ------------------------------- # ✅ create a tuple from user input try: input_list = ast.literal_eval( input('Enter a valid Python tuple, e.g. tasks=("a", "b"): ') ) except ValueError: print('The provided value is not a tuple') print(input_list) # 👉️ ('a', 'b')

create tuple from user input string

We used the input() function to take input from the user.

The input function takes an optional prompt argument and writes it to standard output without a trailing newline.

The function then reads the line from input, converts it to a string and returns the result.

Note that the input() function returns a string even if the user enters a number.

We used the str.split() method to split the user input string on each whitespace character.

main.py
my_tuple = tuple(input('Enter space-separated words: ').split()) print(my_tuple)

The str.split() method splits the string into a list of substrings using a delimiter.

When no separator is passed to the str.split() method, it splits the input string on one or more whitespace characters.

You can also use another string separator, e.g. a comma.

main.py
my_tuple = tuple(input('Enter comma-separated words: ').split(',')) print(my_tuple)

input tuple comma separator

The str.split() method splits the input string on each comma.

If you need to create a tuple of integers from user input, use a generator expression to convert each input string to an integer.

main.py
user_input = input('Enter space-separated integers: ') my_tuple = tuple(int(item) for item in user_input.split()) print(my_tuple)

input tuple integers

We used the str.split() function to split the input string on each whitespace character.

We then used a generator expression to iterate over the list.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the int() class to convert the input string to an integer.

Alternatively, you can use the ast.literal_eval method.

main.py
import ast try: input_list = ast.literal_eval( input('Enter a valid Python tuple, e.g. ("a", "b"): ') ) except ValueError: print('The provided value is not a tuple') print(input_list) # 👉️ ('a', 'b')

input tuple literal

The ast.literal_eval method allows us to safely evaluate a string that contains a Python literal.

The string may consist of strings, bytes, numbers, tuples, lists, dicts, sets, booleans and None.

This assumes that the expected input is a valid Python tuple, e.g. (1, 2, 3).

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.