I want to reverse the string
Input:
I love my country! It is @beautiful
Output:
I evol ym yrtnuoc! tI si @lufituaeb
I want to reverse the string
Input:
I love my country! It is @beautiful
Output:
I evol ym yrtnuoc! tI si @lufituaeb
From each word, using regular expressions, the part without punctuation is extracted, which is reversed and replaced
import re
inp = 'I love my country! It is @beautiful'
out = []
for w in inp.split(' '):
sw = re.search(r'[a-zA-Z]+', w)[0]
out.append(w.replace(sw, sw[::-1]))
print(' '.join(out))
Prints:
I evol ym yrtnuoc! tI si @lufituaeb
Oneliner:
import re
text = 'I love my country! It is @beautiful'
''.join([ s[::-1] for s in re.split(r'(\W)', text)])
Results in:
'I evol ym yrtnuoc! tI si @lufituaeb'