# Function to reverse words of string def rev_sentence(sentence): words = sentence.split(' ') return ' '.join(reversed(words)) if __name__ == "__main__": string = "My Computer Knowledge" print (rev_sentence(string))
Output
$ python3 revwordseq.py Knowledge Computer My |
# Function to reverse words of string string = "My Computer Knowledge" words = string.split() words = list(reversed(words)) print(" ".join(words))
Output
$ python3 revwordseq1.py Knowledge Computer My |
# Function to reverse string string="My Computer Knowledge" txt=string[::-1] print(string,txt)
Output
$ python3 revstr.py My Computer Knowledge egdelwonK retupmoC yM |
# Function to reverse sequence of words in string def reverseword(s): w=s.split(" ") nw=[i[::-1] for i in w] ns=" ".join(nw) return ns string = "My Computer Knowledge" print(reverseword(string))
Output
$ python3 revwords.py yM retupmoC egdelwonK |
also see
C Programming language |
Go Programming language |
Linked List | Array |
Stack | Queue |
Puzzle | Reasoning |
Aptitude | HTML |