Python Type Casting

Casting means changing a data type to another. It is popular in many programming languages like C/C++, Java, etc. Python also supports the type casting and it is done using casting functions. In python, there are two primary casting operations you can do and they are:

  1. Change string to other types such as Integer, Float, Boolean types.
  2. Convert other types to string

Here are a list functions involved in casting from one type to another.

Examples of Type Casting in Python

The example contains following types.

  • Integer to string
  • Float to string
  • Boolean to string

Integer to String

num = 2233
result = str(num)

The output of the above code is

==== RESTART: C:/Python_projects/type casting/ =======
<class 'str'>

Float to String

num_float = 344.55
result = str(num_float)

Once again the output is a string. Note that we are only checking the type and not printing the actual value.

===== RESTART: C:/Python_projects/type casting/ ========
<class 'str'>

Boolean to String

bool = True
result = str(bool)

Here is the output of the above code. Once again it is string, the value True will be "True". Note the double quotes around.

===== RESTART: C:/Python_projects/type casting/ =========
<class 'str'>

Examples of String Type to Other Types

In this section, we will convert other types to its string equivalent.

  • String to integer
  • String to float
  • String to Boolean

String to Integer

str = "Hello World"
str2 = "45"
result = int(str)

The output of the above program is given below. The str cannot be converted into an integer and produce following error.

==== RESTART: C:/Python_projects/type casting/String to =======
Traceback (most recent call last):
  File "C:/Python_projects/type casting/String to", line 3, in <module>
    result = int(str)
ValueError: invalid literal for int() with base 10: 'Hello World'

However, the string “45” is converted to number 45 successfully. See the output below.

==== RESTART: C:/Python_projects/type casting/String to =======
<class 'int'>

String to Float

Now we will check two cases , first we will

  1. convert a number string to float
  2. convert a float number string to float
number_string = "399"
result = float(number_string)

This was successful and the output is below.

======= RESTART: C:/Python_projects/ ===========
<class 'float'>

Now we will convert a float string to float value.

float_string = "3.14"
result = float(float_string)

The output is given below.

======= RESTART: C:/Python_projects/ =========
<class 'float'>

String to Boolean Value

To convert a boolean string such as “True” or “False”, you have to use the ast or Abstract Syntax Trees.

Visit following link to learn more about – ast

It has many utilities function and one of them is ast.literal_aval() that evaluates string to other types such as Boolean.

import ast
boolString = "True"
result = ast.literal_eval(boolString)

The output is given below. Note that the result and its type both are printed.

======= RESTART: C:/Python_projects/ ========
<class 'bool'>