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:
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:
- Hyphenate Letters in Python
- Earthquake in Python | Easy Calculation
- Striped Rectangle in Python
- Perpendicular Words in Python
- Free shipping in Python
- Raj has ordered two electronic items Python | Assignment Expert
- Team Points in Python
- Ticket selling in Cricket Stadium using Python | Assignment Expert
- Split the sentence in Python
- String Slicing in JavaScript
- First and Last Digits in Python | Assignment Expert
- List Indexing in Python
- Date Format in Python | Assignment Expert
- New Year Countdown in Python
- Add Two Polynomials in Python
- Sum of even numbers in Python | Assignment Expert
- Evens and Odds in Python
- A Game of Letters in Python
- Sum of non-primes in Python
- Smallest Missing Number in Python
- String Rotation in Python
- Secret Message in Python
- Word Mix in Python
- Single Digit Number in Python
- Shift Numbers in Python | Assignment Expert
- Weekend in Python
- Shift Numbers in Python | Assignment Expert
- Temperature Conversion in Python
- Special Characters in Python
- Sum of Prime Numbers in the Input in Python