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

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable