String modification in Python

Problem Statement:

We are given a string containing words and numbers(integers). We need to arrange the sort numbers in decreasing order. For example, converting CopyAssignment is 2 years, 5 months, and 2 days old now to CopyAssignment is 5 years, 2 months, and 2 days old now.

Code for String modification in Python:

s = 'CopyAssignment is 2 years, 5 months, and 2 days old now'
l = s.split(' ')
n = []
for i in range(len(l)):
    try:
        l[i] = int(l[i])
        n.append(int(l[i]))
    except:
        pass
n.sort(reverse=True)
j=0
for i in range(len(l)):
    if type(l[i]) is int:
        l[i] = n[j]
        j += 1
for i in range(len(l)):
    l[i] = str(l[i])
print(' '.join(l))

Output:

Output for String modification in Python

Also Read:

Share:

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@violet-cat-415996.hostingersite.com Thank you