Unit 05 · Textos y sus métodos · Lesson 01
Contar caracteres con len()
len() dice cuántos caracteres tiene un texto.
- 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
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?
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.
- Calculá el largo con len(mensaje).
- Mostralo con una f-string: f"El mensaje tiene {len(mensaje)} caracteres."
- 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.
- 1
len() recibe el texto entre paréntesis y da un número.
- 2
Podés poner len(mensaje) directamente entre las llaves de una f-string.
- 3
Queda así: print(f"El mensaje tiene {len(...)} caracteres.").
The key idea
len() cuenta todos los caracteres de un texto, incluidos los espacios.
You passed this lesson
- Mediste textos con len(), incluso uno vacío.
- Ahora vas a conocer los métodos: herramientas que se escriben después del texto, con un punto.
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.