Write instructions
to perform each of the steps below (a)
Create a string containing at least five words and store it in a
variable. (b)
Print out the string. (c)
Convert the string to a list of words using the string split method. (d)
Sort the list into reverse alphabetical order using some of the list methods. (e)
Print out the sorted, reversed list of words. |
# Program
# (a) Create a string containing at least five
words and store it in a variable.
str1 = "I proud to be an Indian"
# (b) Print out the string.
print(str1)
# (c) Convert the string to a list of words using
the string split method.
li = list(str1.split(" ")) print(li)
# (d) Sort the list into reverse alphabetical
order using some of the list methods.
li1 = sorted(li) print(li1) def Reverse(list1) : return
[element for element in reversed(list1)] # (e) Print out the sorted, reversed list of
words. print(Reverse (li1))
|
Output:
I proud to be an Indian ['I', 'proud', 'to', 'be', 'an', 'Indian'] ['I', 'Indian', 'an', 'be', 'proud', 'to'] ['to', 'proud', 'be', 'an', 'Indian', 'I'] |