What is Type Casting in Python?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Before learning about type casting in python, we should be familiar with the data types and data structures in python.

Data types in python are the classification or categorization of the data items (like numbers, integers, strings, etc). Data Types represent the kind of value and also tell what operations can be performed on a particular data (the data can be of any form). We have five built-in (general) data types in python namely - numeric, sequence type, boolean, set, and dictionary.

python data type

So, the conversion of one data type into the other data type is known as type casting in python or type conversion in python. Python supports a wide variety of functions or methods like: int(), float(), str(), ord(), hex(), oct(), tuple(), set(), list(), dict(), etc. for the type casting in python.

There are two varieties of typecasting in python namely - Explicit Conversion(Explicit type casting in python), and Implicit Conversion(Implicit type casting in python). The conversion of one data type into another, done via user intervention or manually as per the requirement, is known as explicit type conversion. On the other hand, in Implicit type conversion, Python automatically converts one data type to another data type without any user's need; quite unique feature of Python language.

Why to Use Type-Casting in Python?

Let us take a real world example to understand the need of type casting in Python. Suppose a person is travelling to America from India and that person wants to buy something from a store in America. But that person only has cash in INR(Indian Rupees). So, the person goes to get their money converted from INR to USD (American Dollar) from a local bank in America. This analogy is simialr to what type casting in python is.

If we have a specified value or an object and we want to convert it into a string object, we use the str() method in python. Similarly, we use the float() method to convert a specified value or an object into a floating-point number or a floating object, and the int() method to convert an object into an integer value or an integer object.

So, to convert one data type into another data type, we use the typecasting in python.

There are two varieties of typecasting in python namely -

  1. Explicit Conversion(Explicit type casting in python), and
  2. Implicit Conversion(Implicit type casting in python).

Refer to the diagram shown below to understand the hierarchy of typecasting in python.

type conversions in Python

The conversion of one data type into another data type, done via developer or programmer intervention or manually as per the requirement, is known as explicit type conversion. It can be achieved with the help of Python’s built-in type conversion functions such as int(), float(), hex(), oct(), str(), etc .

Explicit type conversion is also known as Explicit Type Casting in Python or simply Type Casting in Python.

Let us now talk about implicit type casting in python. As we know that all data types in Python do not have the same level i.e. ordering of data types is not the same in Python. Data types have a certain ordering in which Python handles them. Some of the data types have higher-order, and some have lower order. While performing any operations on variables with different data types in Python, one of the variable's data types will be changed to the higher data type. According to the level, one data type is converted into other by the Python interpreter itself (automatically). This is called, implicit type casting in python. Python converts a smaller data type to a higher data type to prevent data loss.

Examples of Type Casting

Let us now take a few examples to understand how various methods of typecasting in python work.

Addition of String and Integer Using Explicit Conversion

We can add a string and an integer in python. If we convert a string into an integer, we can add both integers to get the desired output. Let us see the example to understand how we can add a string and an integer in python using explicit conversion.

Syntax for conversion of string into integer:

Example:

Output:

Type Casting Int to Float

We can use the built-in float() method in python to convert an integer or string into a floating-point number. The float() method only accepts one parameter that is the value, which needs to be converted into a floating-point.

The syntax of float() method is very simple:

Let us take an example to understand the working of the float() method better. As we know a salary is a floating point number as it salary is added with a lot of allowances. So, we should convert the salary into floating-point number and then perform required operations.

Example:

Output:

Type Casting Float to Int

We can use the built-in int() method in python to convert a specified value or an object into an integer value or an integer object. The int() method accepts two parameters that are the value, which needs to be converted into an integer, and one base valve, which defines the number base. The base parameter is optional. The default value specified is 10. The base-10 value simply resembles the number having base address 10 i.e. decimal numbers.

We can specify the base type of the number other than 10 such as

  • 8 for octal number (to convert octal number into an equivalent decimal number).
  • 16 for hexadecimal number (to convert a hexadecimal number into an equivalent decimal number).
  • 2 for binary number (to convert the binary number into an equivalent decimal number).

The syntax of int() method is very simple:

Let us take an example to understand the working of the int() method better.

Example:

Output:

Type Casting Int to String

We can use the str() function in python to convert a specified value or an object into a string object.

The syntax of the str() method in python is very simple:

The str in python takes three parameters in which one of them is required and the other two are optional. The object is any object or value(s) that is to be converted into a string object. The encoding parameters stores the encoding of the provided object. The error stores the specified action that needs to be performed if the conversion or decoding fails.

Let us briefly discuss the parameters of str() function in python:

  • Object: The object parameter is a required parameter. The object is any object or value(s) that is to be converted into a string object. If we do not provide any object in the parameter, the str in python returns an empty string.
  • Encoding: The encoding parameters stores the encoding of the provided object. If we do not provide any encoding parameter, the default value i.e. UTF-8 is provided.
  • Errors: The error stores the specified action that needs to be performed if the conversion or decoding fails. We can simply say that error is the response when decoding fails.

Let us take an example to understand the working of the float() method better.

Example:

Output:

Type Casting String to Int

As we have seen earlier, we can use the int() method in python to convert a string into an integer.

Syntax:

The value parameter is required but the base parameter is optional.

Let us take an example to see how a string is converted into an integer value in Python. Use this Compiler to compile your Python code.

Output:

Python's simplicity and flexibility make it an ideal language to learn. Join our Free Python course and witness your coding proficiency soar to new heights.

Explore Scaler Topics Python Tutorial and enhance your Python skills with Reading Tracks and Challenges.

Conclusion

  • The conversion of one data type into the other data type is known as type casting in python or type conversion in python.
  • Python supports a wide variety of functions or methods like: int(), float(), str(), ord(), hex(), oct(), tuple(), set(), list(), dict(), etc. for the type casting in python.
  • There are two varieties of typecasting in python namely - Explicit Conversion(Explicit type casting in python), and Implicit Conversion(Implicit type casting in python).
  • The conversion of one data type into another, done via user intervention or manually as per the requirement, is known as explicit type conversion.
  • According to the level of data types, one data type is converted into other by the Python interpreter itself (automatically). This is called, implicit type casting in python.

Learn more :

Refer to the articles below to learn more about different topics that come under the typecasting in python: