×

python · level 2 · unit 3 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

cuts a line of text into a list.

Real data often arrives as one long line of text. .split() cuts it into a list of pieces. With empty brackets it cuts wherever there is , so a sentence becomes a list of words.

Put a in the brackets to cut somewhere else. "tea,milk,jam".split(",") cuts at each comma and gives ['tea', 'milk', 'jam']. The separator itself is thrown away.

Each piece is still a string, even if it looks like a number. Convert it with int(), or with float() for decimals like "4.50".

line = "Sam,12,1.5"
name, age, height = line.split(",")
print(name)
print(int(age) + 1)
print(float(height) * 2)
print("the quick brown fox".split())

float() works like int(), but turns text into a decimal number.