Type Casting in python.

Type Casting

Python characterizes type conversion to straightforwardly change over one data type to another which is valuable in everyday and competitive programming.

There might be times when you need to determine a type on to a variable. This should be possible with casting. Python is an object-orientated language, and as such it utilizes classes to characterize data types, including its primitive types.

Casting in python is done using constructor functions:

  • int() – develops a whole number from a whole number literal, a float literal (by adjusting down to the past integer), or a string literal (giving the string to represent an entire number)
  • float() – develops a floating number from a whole number literal, a float literal or a string literal (allowing the string to represent a float or a number)
  • str() – develops a string from a wide types¬†of data types, including strings, integer literals and float literals
a = int(10.5)
b = float(3)
c = str(123)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))

Output

10
3.0
123
<class 'int'>
<class 'float'>
<class 'str'>

Implicit Type Casting

In Implicit type conversion, Python naturally changes over one data type to another data type. This procedure need not bother with any user involvement.

Let’s understand with an example how Python converts the lower data type (whole number) to the higher data type (float) to avoid data loss.

num1 = 56
num2 = 4.56

num_new = num1 + num2

print(type(num1))
print(type(num2))

print(num_new)
print(type(num_new))

Output

Explicit Type Casting

The process of converting a value to another kind is called type casting. In spite of the fact that certain implicit casting isn’t permitted in Python you will frequently end up defining explicit type conversion inside your code. This happens a lot when you work with the users input.

Imagine, you requested that a user to provide an weight that you will later use in some calculations. To convert a string to integer you can use the built-in int function.

weight = "62"
print(type(weight))  

new_weight = int(weight)
print(type(new_weight))

Output

The type function is used to find out the type of the value provided.

To cast an integer to the string type use str function:

weight = 22
print(type(weight)) 

new_weight = str(weight)
print(type(new_weight))

Output

Except for str and int functions we covered above there is also a float function. It converts a given value to the float type.

Here are some more examples of casting between different types:

a = 5.76  
print(type(a))  
 
s = str(a)  
print(type(s)) 
 
i = int(a) 
print(i)  
print(type(i)) 
 
a = float(i)
print(a)  
print(type(a))

Output

It is important to remember that you can cast the value of any type to a string in Python. This fact is often used in debugging purposes.

Dynamic vs. Static Type Casting

Python is a dynamically and strongly typed language. Dynamic typing implies that just runtime objects (values) have a type, yet not the variables that store them. You can store a few values of various kinds in a single variable during your code execution and no mistakes will happen.

Let’s understand with the help of an example:

avg = 50
avg = 'average'

At first, variable avg is stores an integer value 50 and then it stores a string average.

On the opposite side, in statically typed languages every variable has a type that can’t be changed during the runtime, so the code above would fail. The instances of statically typed languages are C++, Java and Go.

Strong vs. Weak Typing

Strong typing implies that implicit type conversions don’t occur. For instance, despite the fact that “159” comprises just of digits it’s a string. To use it in arithmetic operations you have to change its type to a whole number(integer) or another numerical type. Trying to use it as is leads a TypeError.

"159" + 100

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

If Python had a weak type framework, such value could be interpreted as a whole number to effectively perform out the operation. Such behavior, when one of the operands is implicitly changed over to the type of another operand, is called type coercion.

Since there is no type coercion in Python, a similar operand may give various outcomes depending upon the types of provided operations. For instance, you can add two strings to get a concatenation. It is likewise possible to multiply a string by a whole number:

print(300 + 125) 
 
print("300" + "125") 
 
print(300 * 4)  
 
print("300" * 4)  
 
print("This is a number:", 300)

Output

The example also shows that you can print values of different types if you separate them with commas in the parentheses. The print() function will print all the arguments delimited by a space.