Python tuple() Method
The tuple()
is a constructor of the tuple
class that creates an empty tuple or converts the specified iterable to a tuple.
Syntax:
tuple(iterable)
Parameters:
iterable: (Optional) a sequence or collection to be converted into a tuple.
Return type:
Returns a tuple.
The following example converts iterables into tuples.
tpl = tuple()
print("Empty tuple: ", tpl)
numbers_list = [1, 2, 3, 4, 5]
print("Converting list into tuple: ", tuple(numbers_list))
numbers_tuple = {1, 2, 3, 4, 5}
print("Converting set into tuple: ", tuple(numbers_tuple))
numbers_dict = {1:'one', 2:'two', 3:'three'}
print("Converting dictionary into tuple: ", tuple(numbers_dict))
greet = 'Hello'
print("Converting string into tuple: ", tuple(greet))
Empty tuple: ()
Converting list into tuple: (1, 2, 3, 4, 5)
Converting set into tuple: (1, 2, 3, 4, 5)
Converting dictionary into tuple: (1, 2, 3)
Converting string into tuple: ('H', 'e', 'l', 'l', 'o')