L1·Foundations

🔄Type Conversion

Casting between int, float, str, list, and friends.

castintfloatstr

§ 1Explicit conversion

int(x), float(x), str(x), list(x), tuple(x), set(x), bool(x).

§ 2Watch out

int('3.14') raises ValueError — you must go through float first: int(float('3.14')) → 3.

Example

1n = int("42")
2p = float("3.14")
3chars = list("abc")
4print(n, p, chars)

Try it live · Python runs in your browser

Loading playground…

Checkpoint quiz

Question 1 / 2
int('3.7') does what?