Skip to content
HackoryWEB SECURITY ACADEMYProgramming

Unit 05 · Textos y sus métodos · Lesson 01

Contar caracteres con len()

len() dice cuántos caracteres tiene un texto.

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

Muchos sistemas ponen un límite al largo de un texto: un nombre de usuario, una etiqueta, un mensaje. Antes de guardar un texto, conviene saber cuántos caracteres tiene.

Understand each piece

  • len()

    Cuenta los caracteres de un texto: letras, números, espacios y signos. len("mesa") da 4 y len("dos mesas") da 9, porque el espacio también cuenta.

  • Texto vacío

    "" es un texto sin caracteres. len("") da 0.

Example

mensaje = "Pedido urgente"
print(len(mensaje))

Pedido tiene 6 letras, urgente tiene 7 y hay un espacio en el medio: en total 14 caracteres.

Before moving on: what will happen?

¿Qué va a mostrar este programa?
print(len("Hola Ana"))

Your mission

Medí el mensaje

La prueba crea la variable mensaje con un texto. Mostrá cuántos caracteres tiene, con este formato exacto. Con mensaje = "2 cuadernos", la salida es: El mensaje tiene 11 caracteres.

  1. Calculá el largo con len(mensaje).
  2. Mostralo con una f-string: f"El mensaje tiene {len(mensaje)} caracteres."
  3. Fijate que termine con punto.

If you get stuck

One hint at a time: the first points the way and the last almost solves it.

The key idea

len() cuenta todos los caracteres de un texto, incluidos los espacios.

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.