Type Conversion
Convert between types explicitly with int(), float(), str(), bool(), list() and friends.
Beginner
int("42") # 42
int("ff", 16) # 255 (from hexadecimal)
float("3.3") # 3.3
str(220) # "220"
bool(0), bool("") # False, False — empty/zero values are falsy
list("abc") # ['a', 'b', 'c']
Python also converts implicitly in arithmetic: 1 + 2.5 gives 3.5 (int is widened to float). Invalid conversions raise ValueError.
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.