Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Python
Stage 5: Specialization

Part 02 / Topic 6 of 10

Type casting

Casting means changing a value from one type to another, like changing ₹ notes into coins: same value, different form.

Real-world example: A price comes from a form as text "249" and you need to add GST.

price_text = "249"
price = int(price_text)
print(price * 1.18)          # 293.82

print(int(3.99))             # 3    (cuts the decimal, no rounding)
print(float("3.5"))          # 3.5
print(str(100) + " rupees")  # 100 rupees
print(bool("False"))         # True (any non-empty string is True)
print(int("1010", 2))        # 10   (read text as binary)
Call Works Fails
int() int("42"), int(3.9), int(True) int("3.5"), int("abc") → ValueError
float() float("3.5"), float(2) float("abc")
str() Anything —
bool() Anything (uses truthiness) —

🔍 Going deeper: int() ignores spaces at the ends and allows _ separators (int(" 1_000 ") → 1000). bool("False") is True because only the empty string is falsy; to read yes/no text, compare it: text.strip().lower() == "yes".

Done here? The practice set for Part 2 — 8 exercises with solutions — is on the part page.

Search the guide