DEV Community

Cover image for Ibuprofeno.py💊| #84: Explica este código Python
Cristian Fernando
Cristian Fernando

Posted on

1

Ibuprofeno.py💊| #84: Explica este código Python

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.

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Retry later