Data types and conversions


A data type is the kind of data which will be stored in a variable. Common data types are string, integer, floating point number, character and Boolean.


Python will automatically determine which data type it will store data as. You need to be aware of data types in order to convert data from one type to another.

Data types

Data type Meaning Examples
Integer A whole number 1, 5, 17, 0
Float Floating point number A decimal/real number 5.27, 2.0, 3.14
Boolean True or False True, False
Char A character - one letter or symbol, in Python this is a string) 'H', '7', 'g', '@'
String A list of characters 'Hello', 'Some text'
List A list (similar to an array) ['Earth', 'Fire', 'Water']

Type conversion / casting

Conversion function Convert from what to what Examples
int() string or float to integer int('132'), int(132.0)
float() string or integer to float float('132'), float(132)
str() float or integer to string str(132)

Easy example

age = 15
print('You are ' + str(age))
Show/Hide Output
You are 15


You can also use print('You are ', age) in Python to save needing to do the string conversion and concatenation.


int(string)    #convert string to integer
int(float)     #convert float to integer
float(integer) #convert integer to float
float(string)  #convert string to float
str(integer)   #convert integer to string
str(float)     #convert float to string
type(value)    #find the type of the value given


Example 1 - Creating an integer

numberOfSweets = 10
Show/Hide Output

numberOfSweets will be an integer which stores the value 10.

Example 2 - Creating a floating point number

height = 168.3
Show/Hide Output

height will be a floating point number which stores the value 168.3.

Example 3 - Convert a number into a string for output

numberOfSweets = 10
print('You have ' + str(numberOfSweets) + ' sweets')
Show/Hide Output
You have 10 sweets

Example 4 - Convert a string into a number for arithmetic

numberOfSweetsNow = 10
buySweets = input('How many sweets do you buy? ')
totalSweets = numberOfSweetsNow + int(buySweets)
Show/Hide Output
How many sweets do you buy? 15

Example 5 - Converting variables for inputs and outputs

ageYears = input('How old are you in years? ')
ageWeeks = int(ageYears) * 52
print('You are ' + str(ageWeeks) + ' weeks old.') #convert ageWeeks to a string
Show/Hide Output
How old are you in years? 10
You are 520 weeks old.


You can also do type conversions in the same line of code as an input. For example, ageYears = int(input('How old are you in years? ')).

Example 6 - Find out the data type of a value

Show/Hide Output

<class 'str'>

Example 7 - Find out the data type of a variable

keyColours = ['red', 'green', 'blue']
Show/Hide Output

<class 'list'>

Example 8 - Find the ASCII character number of a character

Show/Hide Output

Example 9 - Find the character of an ASCII number

Show/Hide Output

Key points


Python can use both ‘ and ” for strings. So "hello" and 'hello' are both exactly the same.


You can find out what data type a value or a variable is storing by using type(value).


Python doesn’t have a different data type for strings and characters. It treats a character as a string of length one.