Python Type Casting

At times, we need to specify the data type of a predefined variable, or we need to change the data type of a variable. This is known as type casting.
In computer programming, type casting (or simple ‘casting’) or type conversion is changing the data type of any entity. Example of type casting could be – changing an integer to a float or to a string.

Casting in python is done by using the following three standard inbuilt functions.

int() – This function converts a value to its equivalent integer form. The function would convert an integer to an interger, float to an integer, and a string (provided the string is an integer literal or a float literal) to an integer.

Program to cast a value to integer form

a = int(4)
b = int(2.1)
c = int("7")

print(type(a))
print(type(b))
print(type(c))

Output

<class ‘int’>

<class ‘int’>

<class ‘int’>

float() – This function converts a value to its equivalent decimal form. The function would convert an integer to an integer, float to an integer, and a string (provided the string is an integer literal or a float literal) to a float value.

Example

a = float(4.1)
b = float(2)
c = float("7")
d = float("7.4")

print(type(a))
print(type(b))
print(type(c))
print(type(d))

Output

<class ‘float’>

<class ‘float’>

<class ‘float’>

<class ‘float’>

str() – It converts a value to a string.

Example

a = str("hello4")
b = str(2)
c = str("7")
d = str("7.4")

print(type(a))
print(type(b))
print(type(c))
print(type(d))

Output

<class ‘str’>

<class ‘str’>

<class ‘str’>

<class ‘str’>

However there are some other functions as well, some of them works strictly on specific data types. For example hex() and oct() works specifically on integer types, to convert them to hexadecimal and octal types respectively. Let us have a look on these functions as well.

hex() – This functions converts an integer to its equivalent hexadecimal value.

Example

num = 74
n = hex(num)

print(n)

Output

0x4a

oct() – This function converts an integer to its equivalent octal value.

Example

num = 74
n = oct(num)

print(n)

Output

00112

There are some functions which specifically works on iterable entities. An example of of iterable entity is a string.

set() – This function converts an iterable entity into a set.

Example

word = "hello"
result = set(word)

print(result)

Output

{‘l’, ‘h’, ‘o’, ‘e’}

Note : In the above output, you can see only four values have been inserted into the result set, this is because every value in a set is unique and we had two ‘l’ in the string. It should also be noted that the order of the elements in the above set would be random.

list() – This function converts an iterable entity into a list. For example it converts a string into a list of characters, or it converts a tuple into a list.

Example

word = "hello"
result = list(word)

print(result)

Output

[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]

tuple() – This function converts an iterable entity into a tuple. For example it converts a string into a tuple of characters, or it can convert a list into a tuple.

Example

word = "hello"
result = tuple(word)

print(result)

Output

(‘h’, ‘e’, ‘l’, ‘l’, ‘o’)

Conversion of a character to its ascii value and vice versa

What is ASCII value ?

ASCII stands for American Standard Code for Information Interchange. Every character has a unique ASCII value that is fixed globally. For example the character ‘A’ has the ASCII value 65 and the character ‘a’ has the ASCII value ’97’.

ord() – This function converts a character into its equivalent ASCII value.

Example

x = '$'
result = ord(x)

print(result)

Output

36

chr() – This function converts an integer to its corresponding ASCII character.

Example

x = 68
result = chr(x)

print(result)

Output

D