typestar

Converting between types in Python

Moving values between types with constructor calls.

answer = "42"
number = int(answer)
precise = float(answer)

count = 7
label = str(count) + " items"

pieces = list("abc")
unique = set([1, 1, 2, 3])
frozen = tuple([1, 2, 3])

digit = int("ff", 16)

How it works

  1. int and float parse numbers out of strings.
  2. str turns numbers back into text for display.
  3. list, set, and tuple reshape any iterable.
  4. int with a base parses hex like "ff".

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
197
Tokens
74
Three-star pace
85 tpm

At the three-star pace of 85 tokens a minute, this run takes about 52 seconds.

Type this snippet

Step 4 of 6 in Variables & types, step 4 of 72 in Language basics.

← Previous Next →