Python program to create a tuple from string and list

In this program, we are given a list of strings and a string. We need to create a tuple using the elements of the list and string.
Submitted by Shivang Yadav, on December 15, 2021

While working with complex problems in Python, we need to create new collections that are a combination of elements of the given collection and have different properties that perform different functions as per the programmer's requirement. Here, we will be creating a tuple from string and list in Python.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Create Tuple from String and List in Python

In this article, we will be creating a Python program to create a new tuple of the given string and list in python.

Input:
['Python', 'Programming', 'language']
'Tutorial'

Output: 
('Python', 'Programming', 'language', 'Tutorial')

This task can be performed by adding the values of both collections into a tuple. In Python, there are multiple methods to perform the task.

Method 1:

One method to solve the problem is by adding the string to the original list as a list and then converting the addition list to a tuple using the tuple() method and print the result.

# Python program to create a tuple 
# from string and list in python

# Initializing and printing the list 
# and string collections
stringList = ["Python", "Programming", "Language"]
apndStr = "Tutorial"
print("The elements of the list : " + str(stringList))
print("The string to be appended is " + apndStr)

# Creating a new tuple ƒrom string 
# and list in Python
apndedTuple = tuple(stringList + [apndStr])

print("The tuple after adding values is : " + str(apndedTuple))

Output:

The elements of the list : ['Python', 'Programming', 'Language']
The string to be appended is Tutorial
The tuple after adding values is : ('Python', 'Programming', 'Language', 'Tutorial')

Method 2:

Another method by performing the tuple conversion of both the collections i.e. string and list. And then concatenate the two tuples.

# Initializing and printing the list 
# and string collections

stringList = ["Python", "Programming", "Language"]
apndStr = "Tutorial"

print("The elements of the list : " + str(stringList))
print("The string to be appended is " + apndStr)

# Creating a new tuple ƒrom string 
# and list in Python
apndedTuple = (apndStr, ) + tuple(stringList)

print("The tuple after adding values is : " + str(apndedTuple))

Output:

The elements of the list : ['Python', 'Programming', 'Language']
The string to be appended is Tutorial
The tuple after adding values is : ('Tutorial', 'Python', 'Programming', 'Language')

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.