Tsallaka zuwa abun ciki
Justice Tech School

Type Conversion

Use int(), float(), str(), bool(), list() and tuple() to convert. Bad input raises ValueError, so catch it.

Example
print(int("42") + 1, float("2.5"), str(7) + "!")
print(list("abc"), tuple([1, 2]))
try:
    int("x")
except ValueError:
    print("bad number")

Result

43 2.5 7!
['a', 'b', 'c'] (1, 2)
bad number

Exercise

Convert to text.

(5)

Quiz Test

What does int("x") raise?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.