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

RMAG news

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))

👉 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.

Leave a Reply

Your email address will not be published. Required fields are marked *