AlgoBazaAlgoBaza
Python for kids ยท Module 1

Lesson 16

Changing types: str(), int(), float()

After this lesson: You'll turn text into a number, a number into text, and meet numbers with a dot.

Video

?

The video for this lesson is coming soon.

Explanation

The text "5" and the number 5 look almost the same, but for Python they're two completely different things. Text is just characters - with a number you can calculate. Today you'll meet three magic machines that turn one into the other. ๐Ÿช„

Text or a number?

Remember: what's in quotes is text (type str), and what's without quotes - a number (type int).

  • "5" + "5" joins text: 55,
  • 5 + 5 adds numbers: 10.

They look similar, but Python treats them completely differently.

Text and a number

Look at the boxes: one holds the text '5' (with quotes), the other the number 5 (without quotes). Then see what the plus does with each of them.

not yet
๐Ÿ“ฆ text
not yet
๐Ÿ“ฆ number

Click the arrow and watch the values move between the boxes.

Code
1text = "5"
2number = 5
3print(text + text)
4print(number + number)
Screen
โ–
1/5 ยท line 1 runs next

int() - turns text into a number

The first machine is int() (short for integer - a whole number). You put text with digits into it, and a real number comes out:

  • int("7") is the number 7,
  • int("7") + 3 is 10 - you can calculate!

Without int(): "7" + "3" is 73, because text only gets joined.

The int() machine step by step

Click the arrow: first the machine turns the text into a number, only then Python adds.

int("7") + 3
1/3 ยท click the arrow - Python works out the next part

Joining or adding?

The same digits, two results. First plus glues two texts. Then the int() machine makes numbers out of them - and plus adds them.

๐Ÿ”ค text + text"7"+"3"
"7"โžก๏ธโš™๏ธint("7")โžก๏ธ๐Ÿ“ฆ seven?
"3"โžก๏ธโš™๏ธint("3")โžก๏ธ๐Ÿ“ฆ three?

Click the arrow - the texts go into the machine one by one.

Code
1print("Joined texts:", "7" + "3")
2seven = int("7")
3three = int("3")
4print("Added numbers:", seven + three)
Screen
โ–
1/5 ยท line 1 runs next
?

What does the program print?

number = int("4")
print(number + 1)

Points written as text

The match score was written as the text "40". To add a bonus, first int() makes a number out of it.

1๏ธโƒฃ
๐Ÿ”ค points_text?
โžก๏ธ
โš™๏ธint()
โžก๏ธ
๐Ÿ“ฆ points?

Click the arrow - the program puts a text into a box.

Code
1points_text = "40"
2points = int(points_text)
3print("Points from the match:", points)
4points = points + 2
5print("Points with the bonus:", points)
Screen
โ–
1/6 ยท line 1 runs next

When does the int() machine break?

The int() machine reads the text sign by sign, and every sign must be a digit from 0 to 9. When it meets anything else, it stops.

  1. int("12") - only the digits 1 and 2. The machine makes the number 12 out of them. โœ…
  2. int("two") - the letters t, w, o. We know "two" means 2, but for Python it's just three letters - they're not digits. โŒ
  3. int("2.5") - the two is OK, but then comes a dot. A dot isn't a digit, and besides, int is a whole number - it has no part after a dot. โŒ
  4. int("7a") - one wrong sign is enough to fail. โŒ
TextOnly digits?int() gives
"12"yes ๐Ÿ”ข12 โœ…
"two"no - letters ๐Ÿ”คan error โŒ
"2.5"no - a dotan error โŒ
"7a"no - the letter aan error โŒ

When it fails, the program stops with a ValueError - "wrong value". The type is right (it's text, and int() takes text), but what's inside the text isn't a number.

A word instead of digits

Click the arrow. First int("12") makes the number 12, and print prints it. Then int("two") stops the program - Python runs the lines in order and stops only at the one with the error.

"12"โžก๏ธโš™๏ธint("12")โžก๏ธ๐Ÿ“ฆ number?
"two"โžก๏ธโš™๏ธint("two")โžก๏ธ๐Ÿ“ฆ second_number?

Click the arrow - the texts go into the machine one by one.

Code
1number = int("12")
2print(number)
3second_number = int("two")
4print(second_number)
Screen
โ–
1/4 ยท line 1 runs next

Reading the message

The message is in English - let's take it apart piece by piece:

ValueError: invalid literal for int() with base 10: 'two'
PieceWhat it means
ValueErrorwrong value
invalid literala wrongly written number
for int()for the int() machine
with base 10with normal digits from 0 to 9
'two'this is the text that caused the trouble

Together: "the int() machine can't make a number out of the text 'two'". At the end of the message you always see the culprit - check what in it isn't a digit.

A dot gets in the way of int() too

The text "2.5" has a dot. The int() machine knows only digits, so it jams - a ValueError, with the culprit '2.5' at the end of the message.

"2.5"โžก๏ธโš™๏ธint("2.5")โžก๏ธ๐Ÿ“ฆ whole?

Click the arrow - the texts go into the machine one by one.

Code
1whole = int("2.5")
2print(whole)
Screen
โ–
1/2 ยท line 1 runs next

float() - numbers with a dot

Not everything can be counted in wholes: ice cream costs 3.5 zloty, and half a pizza is 0.5. Such numbers are the type float. In Python we write them with a dot, not a comma!

The second machine, float(), turns text into a number with a dot:

  • float("3.5") is 3.5,
  • float("4") is 4.0 - even a whole number gets .0 at the end.

Remember division / from lesson 5? 10 / 4 is 2.5, and 10 / 2 is 5.0 - the result of / is always a float.

Two ice creams ๐Ÿฆ

The price came as the text "3.5" - we keep it in the box price_text. float() makes a number out of it and puts it into a second box, price_float. Only with the number can we work out two ice creams.

not yet
๐Ÿ“ฆ price_text
not yet
๐Ÿ“ฆ price_float

Click the arrow and watch the values move between the boxes.

Code
1price_text = "3.5"
2price_float = float(price_text)
3print(price_float)
4print(price_float * 2)
Screen
โ–
1/5 ยท line 1 runs next

float() knows the dot, int() doesn't

Remember how int() jammed on "2.5"? The same text twice: float() knows the dot, so it makes the number 2.5 out of it. int() doesn't know the dot - a ValueError.

"2.5"โžก๏ธโš™๏ธfloat("2.5")โžก๏ธ๐Ÿ“ฆ with_dot?
"2.5"โžก๏ธโš™๏ธint("2.5")โžก๏ธ๐Ÿ“ฆ whole?

Click the arrow - the texts go into the machine one by one.

Code
1with_dot = float("2.5")
2print(with_dot)
3whole = int("2.5")
4print(whole)
Screen
โ–
1/4 ยท line 1 runs next
?

Which line stops the program with a ValueError?

A whole number with a dot

float() of a whole number also makes a float - that's why .0 appears on the screen.

Code
1print(float("4"))
2print(float("4") + 1)
3print(10 / 2)
Screen
โ–
1/4 ยท line 1 runs next
?

What does the program print?

print(float("2"))

int() cuts off what's after the dot

You can also put a number with a dot into int(). Then the machine cuts off everything after the dot - it doesn't round!

  • int(7.9) is 7 (not 8),
  • int(2.5) is 2.

Mind the difference: int(7.9) - a number with a dot - works. But int("7.9") - text with a dot - is a ValueError, like "2.5" above.

From float to int - the int() scissors โœ‚๏ธ

A number with a dot (float) goes into int() and a whole number (int) comes out. int() works like scissors: it cuts off everything after the dot. Even 7.9 - although it's close to 8.

Click the arrow - the int() scissors cut off everything after the dot.

Code
1float_number = 7.9
2int_number = int(float_number)
3print(int_number)
4float_number = 2.5
5int_number = int(float_number)
6print(int_number)
7float_number = 3.0
8int_number = int(float_number)
9print(int_number)
Screen
โ–
1/10 ยท line 1 runs next

str() - turns a number into text

Remember the error from lesson 13? "Age: " + 10 is a TypeError - text and a number can't be joined. The third machine, str() (from string - text), turns a number into text:

  • str(10) is the text "10",
  • "Age: " + str(10) is Age: 10 - now it's two pieces of text, so the plus joins them.

A plus without str()

First the error: Python doesn't know if you want to join or to add.

?๐Ÿ”ค text
+
๐Ÿ”ข number
?

Click the arrow - let's see what the plus does with text and a number.

Code
1age = 10
2print("Age: " + age)
Screen
โ–
1/3 ยท line 1 runs next

A plus with str()

First the number goes through the str() machine and becomes text. Then plus joins two texts into a new box sentence - and everything works.

Click the arrow - the number goes through the str() machine.

Code
1age = 10
2age_text = str(age)
3sentence = "Age: " + age_text
4print(sentence)
Screen
โ–
1/5 ยท line 1 runs next

The str() machine step by step

str(2) + str(3)
1/3 ยท click the arrow - Python works out the next part
?

What does the program print?

print(str(2) + str(3))

Three machines together

MachineWhat it doesExample
int()makes a whole numberint("12") is 12
float()makes a number with a dotfloat("12") is 12.0
str()makes textstr(12) is "12"

After the change the same plus does something different: it joins text and adds numbers.

One 12, four results

The same text "12" goes through different machines. Look at the types in the boxes (str, int, float) and at what the plus does.

๐Ÿ“ฆ text?
๐Ÿ“ฆ int_number?
๐Ÿ“ฆ float_number?
๐Ÿ“ฆ number_13?
๐Ÿ“ฆ text_13?

Click the arrow - boxes of different types are made from one text.

Code
1text = "12"
2int_number = int(text)
3float_number = float(text)
4number_13 = int_number + 1
5text_13 = str(number_13)
6print("Joined texts:", text + text)
7print("Added int numbers:", int_number + int_number)
8print("Added float numbers:", float_number + float_number)
9print("Joined texts:", text_13 + text_13)
Screen
โ–
1/10 ยท line 1 runs next

What's it all for?

Remember input()? It always gives text - even when someone types a number. In the next lesson you'll turn the text from input() into a number with the int() machine and calculate with numbers typed on the keyboard!

Your number in two machines โŒจ๏ธ

Type a number (e.g. 21) and press Enter โŽ. input() gives text - see what * 2 does to it, and what it does to the same number after int().

1๏ธโƒฃ
โŒจ๏ธ number_str?
โžก๏ธ
โš™๏ธint()
โžก๏ธ
๐Ÿ“ฆ number?

Type a number on the screen and press Enter - input() gives it as text.

Code
1print("Type a number:")
2number_str = input()
3number = int(number_str)
4print("Text times 2:", number_str * 2)
5print("Number times 2:", number * 2)
Screen
โ–
1/3 ยท line 1 runs next

Remember

  • int("7") is the whole number 7; int(7.9) cuts it to 7.
  • float("3.5") is the number with a dot 3.5; float("4") is 4.0.
  • str(10) is the text "10" - it can be joined with other text using a plus.
  • Text + text joins, number + number adds, text + number is a TypeError.

You've got all three magic machines! Time for the tasks. ๐Ÿช„

Tasks from the video

We solve them together in the video - pause it and try first on your own.

Practice tasks

Try to solve them without the video. Stuck? Every task has hints and an editorial.

Sign in to save your progress.