# Datatype casting

Python determines the datatype automatically, to illustrate:

```x = 3 y = "text"```

It finds x is of type integer and y of type string.

Functions accept a certain datatype. For example, print only accepts the string datatype.

Related course
Complete Python Bootcamp: Go from zero to hero in Python

## Datatypes casting

If you want to print numbers you will often need casting.

In this example below we want to print two numbers, one whole number (integer) and one floating point number.

```x = 3 y = 2.15315315313532   print("We have defined two numbers,") print("x = " + str(x)) print("y = " + str(y))```

We cast the variable x (integer) and the variable y (float) to a string using the str() function.

What if we have text that we want to store as number? We will have to cast again.

```a = "135.31421" b = "133.1112223"   c = float(a) + float(b) print(c)```

In the example above we cast two variables with the datatype string to the datatype float.

## Conversion functions

To convert between datatypes you can use:

Function Description
int(x) Converts x to an integer
long(x) Converts x to a long integer
float(x) Converts x to a floating point number
str(x) Converts x to an string. x can be of the type float. integer or long.
hex(x) Converts x integer to a hexadecimal string
chr(x) Converts x integer to a character
ord(x) Converts character x to an integer

## 16 thoughts on “Datatype casting”

1. - November 7, 2015

Thank you

2. - August 4, 2015

In print “x = ” + str(x) couldn’t we replace the “+” for a comma?

1. - August 4, 2015

Hi Bruno, thanks for your comment! This outputs the same but does not concatenate the string data. If you only want to ouput, this is fine. If you want to do operations on the string later, you can store the output in a string value that way.

3. - July 9, 2015

In the paragraph: An example of casting datatypes in Python:

There is a typo in the sentence “In this example below we weant to print”

I really like this tutorial thanks for making it!

1. - July 9, 2015

Thanks Mason! I updated it

4. - June 19, 2015

sum2 = int(num) + int(flt)
print sum2.

I tried this and got an error message. Is anything wrong in this?

1. - June 19, 2015

Hi, which error message did you get? Are you using python on desktop or on the web?

This code works on my machine (Python 2.5, Python 2.7):

```#!/usr/bin/env python   num = 2 flt = 3.5 sum2 = int(num) + int(flt) print sum2```

for Python 3 it works with:

```#!/usr/bin/env python   num = 2 flt = 3.5 sum2 = int(num) + int(flt) print(sum2)```
5. - June 15, 2015

a = “135.31421”
b = “133.1112223”

c = float(a) + float(b)
print c

In the above example, instead if I try the below statement, what will happen?

print float(a) + float(b)

1. - June 16, 2015

Hi,this will output the sum of both if you use the quote (“) and python 2.7

6. - June 14, 2015

Why we need to insert + before str(x) on the following example:
print “x = ” + str(x)

thanks

1. - June 14, 2015

Hi, the + is for concatination, it adds two strings together. Print takes only one argument, thus we combine the string “x=” and str(x) into one string with the + operator.

Some more details on datatype casting (int to string):

Back in the early days of computing they had only binary numbers (0s or 1s) and wanted a way to print characters to the screen. There was no such way and they made a method that maps binary numbers to latin characters known as the ASCII table. In this table every byte (8 bits) is a character. This means if you print the number 102842 you print individual bytes ‘1’, ‘0’, ‘2’, ‘8’, ‘4’ and ‘2’ in ASCII representation to the screen. An integer is a raw block of memory, usually only 4 bytes and can hold this same value in those 4 bytes.

Let’s do an example. The value 128 can be stored in a single byte using this code: print int(‘10000000’, 2). This byte would take just 8 bits (1s or 0s) in computer memory. To print it, Python uses the ASCII table. Every character is expressed as 1 byte. That means to show the same value on the screen, the computer needs 1 byte for ‘1’, ‘2’ and ‘8’.

To print the ASCII table you can use this code:

```#!/usr/bin/env python   # print ASCII table for i in range(0,128): print 'character: \"' + chr(i), print '\" byte value=' + str(i), print "binary value in computer " + bin(i)```
1. - June 24, 2016

Nice example

7. - May 22, 2015

what is the difference between the usage of single inverted commas ( ‘ ‘ ) and double inverrted commas ( ” ” ).
thanks

1. - May 22, 2015

Both the single commmas (‘) and double commas (“) are equal and what you use is what you prefer.

8. - May 22, 2015

hello
I didn’t understand the significance of casting. I intend I could have directly give the command print c to print the value of c in example 1. Why did I opt to use datatype casting ?

1. - May 22, 2015

There are situations in which casting when printing is neccesary. If you want to print integers in combinations with strings or in many other situations. The casting will convert say the integer to a string for printing. If you can print without casting, you should, but this is not always the case.