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
intandfloatparse numbers out of strings.strturns numbers back into text for display.list,set, andtuplereshape any iterable.intwith a base parses hex like"ff".
Keywords and builtins used here
floatintlistsetstrtuple
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.
Step 4 of 6 in Variables & types, step 4 of 72 in Language basics.