KnowledgeBoat Logo

Computer Science

Write a Python program that accepts a hyphen-separated sequence of words as input and prints the words in a hyphen-separated sequence after sorting them alphabetically.

Sample Items: green-red-yellow-black-white
Expected Result: black-green-red-white-yellow

Python

Python Functions

2 Likes

Answer

def sort_words(s):
    words = s.split('-')
    words.sort()
    return '-'.join(words)

input_str = input('Enter a hyphen-separated sequence of words: ')
print(sort_words(input_str))

Output

Enter a hyphen-separated sequence of words: green-red-yellow-black-white   
black-green-red-white-yellow

Enter a hyphen-separated sequence of words: delhi-bangalore-mumbai-chennai-lucknow-patna
bangalore-chennai-delhi-lucknow-mumbai-patna

Answered By

2 Likes


Related Questions