Converting Data Types in Python
Python has several built-in functions for converting the types of our variables.
For example, we have the int() function for converting a value to an integer:
birth_year = "1982"age = 2020 - int(birth_year)print("You are " + str(age) + "
years old.")
In the above example, we use the int() function to convert the birth_year string to
an integer so we can subtract it from the current year.
We also have the float() function for converting a value to a float:
num = "10.5"float_num = float(num)print(float_num)
In the above example, we use the float() function to convert the num string to a
floating point number.
Type Conversion in Python
In programming languages like Python, there are times when you need to convert a
variable from one type to another. In this tutorial, I'll show you a bunch of cool
things you can do with strings using Python for beginners.
In Python, the index of the first character in a string is 0. So, when we run this
program, you're going to see 1 on the terminal because the index is 1.
Python has several built-in functions for converting the types of our variables.
For example, we have the int() function for converting a value to an integer:
birth_year = "1982"age = 2020 - int(birth_year)print("You are " + str(age) + "
years old.")
In the above example, we use the int() function to convert the birth_year string to
an integer so we can subtract it from the current year.
We also have the float() function for converting a value to a float:
num = "10.5"float_num = float(num)print(float_num)
In the above example, we use the float() function to convert the num string to a
floating point number.
Type Conversion in Python
In programming languages like Python, there are times when you need to convert a
variable from one type to another. In this tutorial, I'll show you a bunch of cool
things you can do with strings using Python for beginners.
In Python, the index of the first character in a string is 0. So, when we run this
program, you're going to see 1 on the terminal because the index is 1.