• doesn`t work with regular and reverse words

Question related to mission Backward Each Word

 

Task:

In a given string you should reverse every word, but the words should stay in their places. Input: A string. Output: A string. Precondition: The line consists only from alphabetical symbols and spaces.

I tried this one:

text: ('Hello      world') #with many spaces between words

li2:   ['Hello', 'world']

li:  ['olleH', 'dlrow']

and then replace in original text:

text = text.replace(li2[i],li[i])

code

def backward_string_by_word(text: str) -> str:
    li = []
    li2 = text.split()
    #revers all words
    for a in text.split():
        li.append(a[::-1])
    #word replacement in text
    for i in range(0,len(li2)):
       text = text.replace(li2[i],li[i])
    return text

Problem:

This code works in all asserts:

assert backward_string_by_word('') == ''
assert backward_string_by_word('world') == 'dlrow'
assert backward_string_by_word('hello world') == 'olleh dlrow'
assert backward_string_by_word('hello   world') == 'olleh   dlrow'
assert backward_string_by_word('welcome to a game') == 'emoclew ot a emag'

But code does not work if string has a regular word and its reverse:

(backward_string_by_word('iH    Hi')) == 'iH    iH'
(backward_string_by_word('Hello olleH')) == 'Hello Hello'
(backward_string_by_word('Hello 2olleH')) == 'olleH Hello2'
(backward_string_by_word('Hello olleH hi ih yes s2ey code')) =='Hello Hello hi hi sey ye2s edoc'

What do I need to know to understand the cause of the problem or what am I doing wrong?