Log in from a computer to take this course

You'll need to log in from a computer to start Learn Python 2. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store

Python automatically assigns a variable the appropriate datatype based on the value it is given. A variable with the value 7 is an integer, 7. is a float, "7" is a string. Sometimes we will want to convert variables to different datatypes. For example, if we wanted to print out an integer as part of a string, we would want to convert that integer to a string first. We can do that using str():

age = 13 print "I am " + str(age) + " years old!"

This would print:

>>> "I am 13 years old!"

Similarly, if we have a string like "7" and we want to perform arithmetic operations on it, we must convert it to a numeric datatype. We can do this using int():

number1 = "100" number2 = "10" string_addition = number1 + number2 #string_addition now has a value of "10010" int_addition = int(number1) + int(number2) #int_addition has a value of 110

If you use int() on a floating point number, it will round the number down. To preserve the decimal, you can use float():

string_num = "7.5" print int(string_num) print float(string_num)
>>> 7 >>> 7.5



Create a variable called product that contains the result of multiplying the float value of float_1 and float_2.


Create a string called big_string that says:

The product was X

with the value of product where the X is.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?