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:

Example 4 - Teachoo.jpg

Ask a doubt
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.