Thursday, February 3, 2022

Reverse words Codewars Python

Description:

Complete the function that accepts a string parameter, and reverses each word in the string. All spaces in the string should be retained.

Examples

"This is an example!" ==> "sihT si na !elpmaxe"
"double  spaces"      ==> "elbuod  secaps"

def reverse_words(text):
    new=[]
    for word in text.split(' '):
        new.append(word[::-1])
    return ' '.join(new)

def reverse_words(str):
    return ' '.join(s[::-1] for s in str.split(' '))

# Split the string with space delimiter and reverse each word and append


Related Posts


No comments :

Post a Comment