Python program to perform concatenation of two string tuples

In this program, we are given two tuples with string elements. We need to create a Python program to concatenate the string elements of the tuples.
Submitted by Shivang Yadav, on December 12, 2021

While working with python collections with elements, we might require operations to extract some information as a combination of tuple elements. In this program, we will perform the concatenation of two strings which are elements of the tuple.

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)

Concatenation of two string tuples

In this article, we are given two tuples with string elements. We will be creating a python program to perform concatenation of two string tuples.

Input:
tup1 = ('python', 'learn', 'web')
tup2 = ('programming', 'coding', 'development')

Output:
('python programming', 'learn coding', 'web development')

One method to solve the problem is by iterating over the tuples and concatenating the string.

This can be done in python in multiple ways using its built-in methods,

Method 1:

One method to solve the problem is by using the + operator to concatenate the strings. And use zip along with the generator expression to create a new tuple with string concatenated.

# Python program to perform concatenation 
# of two string tuples

# Initialing and printing tuples
strTup1 = ("python", "learn", "web")
strTup2 = (" programming", " coding", " development")
print("The elements of tuple 1 : " + str(strTup1))
print("The elements of tuple 2 : " + str(strTup2))

# Performing concatenation of string tuples 
concTup = tuple(str1 + str2 for str1, str2 in zip(strTup1, strTup2))

print("The tuple with concatenated string : " + str(concTup))

Output:

The elements of tuple 1 : ('python', 'learn', 'web')
The elements of tuple 2 : (' programming', ' coding', ' development')
The tuple with concatenated string : ('python programming', 'learn coding', 'web development')

Method 2:

Another method to solve the problem is by using concat method to concatenate string values from tuples. Then map these values then convert them to the tuple.

The concat method is imported from the operator library.

# Python program to perform concatenation 
# of two string tuples

import operator 

# Initialing and printing tuples
strTup1 = ("python", "learn", "web")
strTup2 = (" programming", " coding", " development")
print("The elements of tuple 1 : " + str(strTup1))
print("The elements of tuple 2 : " + str(strTup2))

# Performing concatenation of string tuples 
concTup = tuple(map(operator.concat, strTup1, strTup2))

print("The tuple with concatenated string : " + str(concTup))

Output:

The elements of tuple 1 : ('python', 'learn', 'web')
The elements of tuple 2 : (' programming', ' coding', ' development')
The tuple with concatenated string : ('python programming', 'learn coding', 'web development')

Python Tuple Programs »



Related Programs




Comments and Discussions!

Load comments ↻






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