Word Mix in Python | Assignment Expert

Problem Statement:

We are given a sentence, we need to word mix in Python means we need to mix the words of the sentence such that new words get formed. How? We need to select a letter from each word from the same index. If there is no letter in the word, do nothing and proceed further.

For example, sentence=> “Welcome to CopyAssignment”=> WtC eoo lp cy oA ms es i g n m e n t

Code for Word Mix in Python:

sentence = 'Welcome to CopyAssignment'
words = sentence.split()
max_len_list = max(words, key = len)
max_len = len(max_len_list)
new_words=[]
for i in range(max_len): # for looping max word length times
    new_word=[]
    for j in range(len(words)): # for looping numbers of words time
        try:
            new_word.append(words[j][i])
        except:
            pass
    new_words.append(new_word)
word_list=[]
for i in new_words:
    word_list.append(''.join(i))
for i in word_list:
    print(i, end="   ")

Output:

Word Mix in Python | Assignment Expert

Explanation:

We have accessed the letters of each word of the sentence, but not all words do not have the same numbers of letters that are why we have used the try and except block of Python. It saved us from throwing error even when we are accessing a string index that does not exist. We hope you find your solution to Word Mix in Python.


Also Read:

Share:
Avatar of Harry

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you