Creating a Tuple or a Set from user Input in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Table of Contents

  1. Creating a Tuple from user Input in Python
  2. Creating a Set from user Input in Python

# 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 tuple from user input string

The code for this article is available on GitHub

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 the 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.

# Creating a tuple of integers from user input

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

The code for this article is available on GitHub

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.

# Creating a tuple from user input with literal_eval

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 code for this article is available on GitHub

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).

# Creating a Set from user Input in Python

To create a set object 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 set() class to convert the list to a set object.
main.py
import ast # โœ… create a set from string user input my_set = set(input('Enter space-separated words: ').split()) print(my_set) # ------------------------------- # โœ… create a set from integer user input user_input = input('Enter space-separated integers: ') my_set = set(int(item) for item in user_input.split()) print(my_set)

create set from user input

The code for this article is available on GitHub

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 the 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_set = set(input('Enter space-separated words: ').split()) print(my_set)

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_set = set(input('Enter comma-separated words: ').split(',')) print(my_set)

create set from user input comma separator

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

# Creating a set of integers from user input

If you need to create a set 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_set = set(int(item) for item in user_input.split()) print(my_set)

create set of integers from user input

The code for this article is available on GitHub

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.

# Creating a set from user input with literal_eval()

Alternatively, you can use the ast.literal_eval method.

main.py
import ast try: input_list = ast.literal_eval( input('Enter a valid Python set, e.g. {"a", "b"}: ') ) except ValueError: print('The provided value is not a set object') print(input_list) # ๐Ÿ‘‰๏ธ ('a', 'b')

create set from user input literal

The code for this article is available on GitHub

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 set, e.g. {1, 2, 3}.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev