Pythondex

Tutorial

Type Conversion in Python

In Python, values taken from input() are always strings. Sometimes we need to convert them to other types like integers or floats. You can use type() to check the type before and after conversion.

Convert to Integer

Convert to Float

Convert to String

Convert Boolean to String

Example with Input

input() always returns a string. So we need to use int() or float() to convert numeric input.

Explanation

  • int() converts a string or float to an integer
  • float() converts a string or integer to a float
  • str() converts numbers or booleans to a string
  • type() shows the data type of a variable