Skip to content
HackoryWEB SECURITY ACADEMYProgramming

Unit 07 · Listas · Lesson 06

De texto a lista con split()

split() corta un texto y devuelve una lista.

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

Algunos formularios guardan varios valores en un solo texto, separados por comas: cuaderno,lápiz,regla. Para trabajar con cada valor hay que separarlos.

Understand each piece

  • texto.split(",")

    Corta el texto en cada coma y devuelve una lista con los pedazos: "teclado,mouse".split(",") da ['teclado', 'mouse'].

  • Siempre textos

    Los pedazos son textos, aunque parezcan números: "2,5".split(",") da ['2', '5'].

  • Sin separador

    Si el texto no tiene comas, split devuelve una lista con un solo elemento: "teclado".split(",") da ['teclado'].

Example

etiquetas = "python,datos,web"
lista = etiquetas.split(",")
print(lista)
print(len(lista))

split corta el texto en las dos comas y arma una lista de tres textos. len da 3.

Before moving on: what will happen?

¿Qué va a mostrar este programa?
print("2,5,10".split(","))

Your mission

Separá la lista de productos

La prueba crea la variable mensaje con productos separados por comas, sin espacios (por ejemplo "cuaderno,lápiz,regla"). Creá la lista productos con split y mostrá estas tres líneas. Con ese mensaje, la salida es: Productos: 3 Primero: cuaderno Último: regla

  1. productos = mensaje.split(",").
  2. La cantidad es len(productos).
  3. El primero es productos[0] y el último, productos[-1].

If you get stuck

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

The key idea

split corta un texto en cada separador y devuelve una lista de textos.

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.