Changing the data type of a variable in Python from one type to another , as and when required, is called type conversion.

Type conversion is of two types:

  • Explicit Conversion
  • Implicit Conversion

Explicit Conversion

Explicit conversion, also called type casting , happens when data type conversion takes place because the programmer forced it in the program. 

The general form of an explicit data type conversion is: 

(new_data_type) (expression)

Examples:

Function

Description

int(x)

Converts x to an integer

float(x)

Converts x to a floating point number

str(x)

Converts x to a floating point representation

chr(x)

Converts ASCII value of x to character

 

Implicit Conversion

Implicit conversion, also known as coercion, happens when data type conversion is done automatically by Python and is not instructed by the programmer.

Example:

Type Conversion: Changing Data Types in Python - Teachoo - Concepts

Remove Ads
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.