Skip to content
HackoryWEB SECURITY ACADEMYProgramming

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

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.

Python downloads when you start reading.
  1. 01Understand
  2. 02Try
  3. 03Explain

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?

¿Qué va a mostrar este programa?
total = 90
print("Pagás $" + str(total))

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.

  1. Convertí total en texto con str(total).
  2. Uní las partes con +: usuario, ", tu pedido cuesta $", el total convertido y ".".
  3. 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.

The key idea

Para unir un número a un texto con +, primero lo convertís con str().

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.