Type conversions (Also known as Type Casting) in Python
pulkitgovrani
Posted on June 16, 2024
When we convert the value of one datatype into another datatype then it is known as type conversion. There are two types of type conversions:
- Implicit Type Conversion
- Explicit Type Conversion
Implicit Type Conversion
In this type conversion python automatically converts a value of one datatype into another datatype. It will convert datatype having lower preference to the datatype having higher preference.
Code Example:
Output:
Since float has higher preference so the output will also be in float. This is very useful because we will not loose our data.
But there are some values of one datatypes which can be converted another implicitly. That’s why we have explicit type conversions.
Converting integer to string
Code
Output
Explicit Type Conversion
In explicit type conversion, the user has to mention explicitly to which value of one datatype he/she wants to convert to another datatype. This can be done by using predefined functions like int(), str(), float().
Code
Output
I hope you were able to understand type conversions in python. Do comment below in case of any doubts.
Posted on June 16, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.