World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Use for loop
def reverse_words(s): result = "" new = s.split(" ") for word in new: word = word[::-1]+" " result += word return result.rstrip() #rstrip does not work everywhere; #it only works at the beginning or end of a function # test case reverse_words(s ="India is my Country")
output
"aidnI si ym yrtnuoC"