- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to merge two strings in alternating fashion in Python
Suppose we have two strings s and t of same size. We have to join letters from s and t in alternative fashion. So take s[i] concatenate with t[i] then go for next letter and so on.
So, if the input is like s = "hello" t = "world", then the output will be "hweolrllod"
To solve this, we will follow these steps −
- zipped := perform zip operation on s and t to make pairs like (s[i], t[i])
- zipped := make a list where each element is s[i] concatenate t[i]
- return the zipped list by joining them into a single string.
Example
Let us see the following implementation to get better understanding −
def solve(s, t): zipped = list(zip(s, t)) zipped = map(lambda x: x[0]+x[1], zipped) return ''.join(zipped) s = "hello" t = "world" print(solve(s, t))
Input
"hello", "world"
Output
hweolrllod
- Related Articles
- Program to find largest merge of two strings in Python
- Program to get maximum length merge of two given strings in Python
- Largest Merge of Two Strings in Python
- Merge two arrays with alternating Values in JavaScript
- Program to merge strings alternately using Python
- How to merge two strings alternatively in JavaScript
- Python program to merge two Dictionaries
- Largest Merge of Two Strings in C++
- Python - Ways to merge strings into list
- Python Program to Merge Two Lists and Sort it
- Program to add two numbers represented as strings in Python
- Program to determine if two strings are close in Python
- Program to perform prefix compression from two strings in Python
- Program to merge two sorted list to form larger sorted list in Python
- How to merge two JSON strings in an order using JsonParserSequence in Java?

Advertisements