Type Casting in Python — Python Technical Interview Questions & Answers
Type Casting in Python
Type Casting in Python
- Type casting changes a value’s type. int("10") → 10. str(10) → "10". float("3.5") → 3.5. list("Asha") → ['A','s','h','a']. bool(0) is False; bool("Asha") is True. Empty "", [], {}, None are falsy.
- Bad text fails: int("hi") → ValueError. bool rules show up in if conditions more than in explicit bool() calls. Show one valid cast and one failing cast in the viva.
- Type Casting in Python — output: 22 then 100 marks. "21" became int; 100 became str for join.
Technical Interview Tip: Valid cast + failing cast + falsy list.
Code Examples for Type Casting in Python
Example
# Type casting
n = int("21")
print(n + 1)
print(str(100) + " marks")
Type Casting in Python — output: 22 then 100 marks. "21" became int; 100 became str for join.
How to Explain Type Casting in Python to an Interviewer
Explain Type Casting as if you are teaching a junior — definition, then one tiny script.
int() float() str() bool(). Falsy: 0 "" [] None.
What does the example print, and why?
Type Casting in Python — output: 22 then 100 marks
What mistake do freshers make with Type Casting?
int("hi") → ValueError.