- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Concatenation of two String Tuples in Python
When it is required to concatenate two string tuples, the 'zip' method and the generator expression can be used.
The zip method takes iterables, aggregates them into a tuple, and returns it as the result.
Generator is a simple way of creating iterators. It automatically implements a class with '__iter__()' and '__next__()' methods and keeps track of the internal states, as well as raises 'StopIteration' exception when no values are present that could be returned.
Below is a demonstration of the same −
my_tuple_1 = ('Jane', 'Pink', 'El') my_tuple_2 = ('Will', 'Mark', 'Paul') print ("The first tuple is : " ) print(my_tuple_1) print ("The second tuple is : " ) print(my_tuple_2) my_result = tuple(elem_1 + elem_2 for elem_1, elem_2 in zip(my_tuple_1, my_tuple_2)) print("The concatenated tuple is : ") print(my_result)
The first tuple is : ('Jane', 'Pink', 'El') The second tuple is : ('Will', 'Mark', 'Paul') The concatenated tuple is : ('JaneWill', 'PinkMark', 'ElPaul')
- Two list of tuples (strings) are defined, and displayed on the console.
- The lists are iterated over, and they are zipped using the 'zip' method.
- The first and second elements from both the list of tuples are added/concatenated.
- This is then converted to a tuple.
- This operation is assigned to a variable.
- This variable is the output that is displayed on the console.
- Related Articles
- Return element-wise string concatenation for two arrays of string in Numpy
- String Concatenation by + (string concatenation) operator.
- Python – Incremental Slice concatenation in String list
- Flatten Tuples List to String in Python
- String Concatenation in Java
- Python Program to Merge two Tuples
- What is the most efficient string concatenation method in python?
- Combining tuples in list of tuples in Python
- Check if two list of tuples are identical in Python
- Check if concatenation of two strings is balanced or not in Python
- Concatenation of two strings in PHP\n
- Concatenation of two strings in PHP program
- How do we compare two tuples in Python?
- Count tuples occurrence in list of tuples in Python
- How to do string concatenation without '+' operator in Python?