valid_punctuation = string.punctuation.replace('-', '')
word_pattern = re.compile('([\\w|-]+)([' + valid_punctuation + ']*)$')

def word_reverse(w):
    m = re.match(word_pattern, w)
return ''.join(reversed(m.groups(1)[0])) + m.groups(1)[1]

def sentence_reverse(s):
    return ' '.join([word_reverse(w) for w in re.split('\\s+', s)])
str1 = input('Enter the sentence: ')
print(sentence_reverse(str1))
String reverse in Python