Type Casting and Conversion

Type casting is converting one data type to another. Python makes it simple using built-in functions.

Common Conversions

  • int() – Convert to integer
  • float() – Convert to float
  • str() – Convert to string
  • bool() – Convert to boolean

Examples

num_str = "10"
num = int(num_str)  # 10 as an integer
price = 9.99
price_str = str(price)  # "9.99"

Always ensure the conversion is valid to avoid errors.

← PrevNext →