I am trying to reverse the letters of each words contained in a given string, keeping the same word order and also keeping the same whitespace.
So 'This is an example!'
should return: 'sihT si na !elpmaxe'
(note two spaces between each word).
The solution I wrote doesn't deal with this whitespace:
def reverse_words(str1):
list1 = str1.split()
list2 = []
for e in list1:
e = e[::-1]
list2.append(e)
return ' '.join(list2)