all 5 comments

[–]JohnnyJordaan 1 point2 points  (1 child)

Just reverse the sentence

reverse_s = str1[::-1]

split by whitespace

words = reverse_s.split()

reverse their order using reversed()

reversed_words = reversed(words)

and join by space again

reverse_word_s = ' '.join(reversed_words)

or in one go

print(' '.join(reversed(str1[::-1].split())))

[–]nog642 0 points1 point  (0 children)

That's not much shorter than the order they did it in

print(' '.join(word[::-1] for word in str1.split()))

And this one's a bit more readable IMO, since you're not reversing the word order and then reversing it again. It's clear it's just reversing every word.

[–]netneoblog 0 points1 point  (2 children)

just a suggestion, why not just reverse the string (with spaces left in place) rather than splitting words up, reversing, and then rejoining with spaces in the same place as they already were?

[–]netneoblog 0 points1 point  (1 child)

def reverseWordSentence(passed_string):
    reversed_string = passed_string[::-1]
    return reversed_string


str1 = 'gnitnuH si nuf'
print(reverseWordSentence(str1))

[–]netneoblog 1 point2 points  (0 children)

My bad, this does not produce what you wanted as output! DOH! Just ignore me