Last updated: Apr 9, 2024
Reading timeยท4 min
To create a tuple from user input:
input()
function to take input from the user.str.split()
function to split the input string into a list.tuple()
class to convert the list to a tuple.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)
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.
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.
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.
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.
my_tuple = tuple(input('Enter comma-separated words: ').split(',')) print(my_tuple)
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.
user_input = input('Enter space-separated integers: ') my_tuple = tuple(int(item) for item in user_input.split()) print(my_tuple)
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.
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.
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')
The ast.literal_eval() method allows us to safely evaluate a string that contains a Python literal.
This assumes that the expected input is a valid Python tuple, e.g. (1, 2, 3)
.
To create a set
object from user input:
input()
function to take input from the user.str.split()
function to split the input string into a list.set()
class to convert the list to a set
object.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)
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.
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.
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.
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.
my_set = set(input('Enter comma-separated words: ').split(',')) print(my_set)
The str.split()
method splits the input string on each comma.
set
of integers from user inputIf you need to create a set
of
integers from user input, use a
generator expression to convert each input string to an integer.
user_input = input('Enter space-separated integers: ') my_set = set(int(item) for item in user_input.split()) print(my_set)
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.
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.
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')
The ast.literal_eval() method allows us to safely evaluate a string that contains a Python literal.
This assumes that the expected input is a valid Python set
, e.g. {1, 2, 3}
.
You can learn more about the related topics by checking out the following tutorials: