- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- String Concatenation by + (string concatenation) operator.
- Return element-wise string concatenation for two arrays of string in Numpy
- Concatenation of two strings in PHP
- Concatenation of two strings in PHP program
- String Concatenation in Java
- Python – Incremental Slice concatenation in String list
- Check if two list of tuples are identical in Python
- Combining tuples in list of tuples in Python
- Flatten Tuples List to String in Python
- Check if concatenation of two strings is balanced or not in Python
- How do we compare two tuples in Python?
- Count tuples occurrence in list of tuples in Python
- Python - Intersection of two String
- Remove duplicate tuples from list of tuples in Python
- Find the Maximum of Similar Indices in two list of Tuples in Python