Reverse Words In A Given String In Python Code Example


Example 1: how to reverse word order in python

## initializing the string string = "I am a python programmer" ## splitting the string on space words = string.split() ## reversing the words using reversed() function words = list(reversed(words)) ## joining the words and printing print(" ".join(words))

Example 2: reverse each word in a string python

def reverse_word_sentence (sentence):    return ' '.join(word[::-1] for word in sentence.split(" "))   # Input: "Split Reverse Join" # Output: "tilpS esreveR nioJ"

Example 3: reverse words in a given string

function reverse (word) {     word = word.split('.').reverse().join('.')     return word  } word = 'i.like.this.program.very.much' word = 'pqr.mno' console.log(reverse(word))

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?