Ibuprofeno.py馃拪| #84: Explica este c贸digo Python

Cristian Fernando - Apr 18 - - Dev Community

Explica este c贸digo Python

Dificultad: Intermedio

def f(text, target_word, replace_word):
    index_target_word = text.find(target_word)
    x = text[0:index_target_word]
    y = text[index_target_word + len(target_word):len(text)]
    return f"{x}{replace_word}{y}"

print(f("Keep calm and write Python", "write", "enjoy"))
Enter fullscreen mode Exit fullscreen mode

馃憠 A. Keep calm and write Python
馃憠 B. Keep calm and enjoy Python
馃憠 C. IndexError
馃憠 D. TypeError


Respuesta:

馃憠 B. Keep calm and enjoy Python
La funci贸n f implementa un algoritmo para buscar y remplazar una palabra dada por otra sin el uso de la funci贸n replace de Python.

Usando find() y dividiendo la cadena podemos lograr lo que se pretende.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .