Create a tuple from string and list in Python


When it is required to create a tuple from a string and a list, the tuple method can be used.

A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).

Below is a demonstration of the same −

Example

Live Demo

my_list_1 = ['Hey', 'there', 'How', 'are', 'you']
my_list_2 = 'Jane'

print("The first list is :")
print(my_list_1)

print("The string is :")
print(my_list_2)

my_result = tuple(my_list_1 + [my_list_2])
print("The aggregated tuple is : ")
print(my_result)

Output

The first list is :
['Hey', 'there', 'How', 'are', 'you']
The string list is :
Jane
The aggregated tuple is :
('Hey', 'there', 'How', 'are', 'you', 'Jane')

Explanation

  • A list is created and displayed on the console.
  • Another string is defined.
  • The tuple and '[]' are used to aggregate the list and string.
  • The final output is displayed on the console.

Updated on: 11-Mar-2021

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements