Converting types
Call the type to convert: int("42"), float("2.5"), str(7). int
ignores spaces around the number, so int(" 42\n") works.
Some surprises:
int("3.5")raisesValueError. Go throughfloatfirst.int(-3.7)is-3. It cuts toward zero, while//rounds down.bool("False")isTrue, because any non-empty string is truthy.int("ff", 16)reads other bases, andint("1_000")allows underscores.
When input might be bad, catch ValueError. Parsing user text is a common
interview step, and it is where these rules bite.
Run it, then try int("") and float("nan").