Unit 04 · Datos de entrada simulados · Lesson 04
Convertir número a texto con str()
str() convierte un número en texto para unirlo con +.
- Difficulty: Fácil
- 10 XP
- Python
- Not started
Lesson content is in Spanish.
Python in your browser
The first time, Python downloads about 5 MB (only once; then it stays saved). If you can, use Wi-Fi.
Python is already saved in this browser: it will not download again.
The situation
Una notificación tiene que decir Ana, tu pedido cuesta $45., con el signo $ pegado al número. Con una coma, print pondría un espacio en el medio. Con +, Python se queja si mezclás texto y número.
Understand each piece
str()Convierte un número en texto: str(45) da "45". Así lo podés unir con otros textos.
TypeError"Total: $" + 45 se detiene con TypeError: + no puede unir un texto con un número. La solución es "Total: $" + str(45).
Coma o +La coma separa con un espacio: print("$", 90) muestra $ 90. El + pega sin espacio: print("$" + str(90)) muestra $90.
Example
total = 45
mensaje = "Total: $" + str(total)
print(mensaje)
str(total) convierte 45 en el texto "45". + lo pega a "Total: $" sin espacios. Se muestra Total: $45.
Before moving on: what will happen?
Your mission
Armá la notificación
La prueba crea dos variables: usuario (un texto) y total (un número entero). Mostrá una notificación en una sola línea, unida con + y str(), sin espacio entre el $ y el número. Con usuario = "Ana" y total = 45, la salida tiene que ser: Ana, tu pedido cuesta $45.
- Convertí total en texto con str(total).
- Uní las partes con +: usuario, ", tu pedido cuesta $", el total convertido y ".".
- Mostralo con un solo print.
If you get stuck
One hint at a time: the first points the way and the last almost solves it.
- 1
+ solo une textos con textos. total es un número.
- 2
str(total) convierte el total en texto.
- 3
Queda así: print(usuario + ", tu pedido cuesta $" + str(total) + ".").
The key idea
Para unir un número a un texto con +, primero lo convertís con str().
You passed this lesson
- Armaste un mensaje exacto uniendo textos y un número convertido.
- Con muchas partes, los + se vuelven difíciles de leer. En la próxima lección vas a conocer una forma más cómoda: las f-strings.
Solve with help
If you have already tried, you can see a reference solution. The lesson is marked as solved with help and adds no XP.