World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITian's, only for AI Learners.
Download our e-book of Introduction To Python
4 (4,001 Ratings)
218 Learners
Shashank Shanu
2 years ago
# import library
import random
import re
# creating a function to perform the task
def replace(a):
word = list(a.group(2))
random.shuffle(word)
return a.group(1) + "".join(word) + a.group(3)
text = "Hello, You should reach the finish line."
print(re.sub(r"(\w)(\w+)(\w)", replace, text))
print(re.sub(r"(\w)(\w+)(\w)", replace, text))
Hlleo, You slouhd raech the fsiinh lnie.
Hlleo, You suolhd raceh the fniish line.