How To Convert String To Int In Python
Last Updated : Mar 11, 2024
In this tutorial we will show you the solution of how to convert string to int in python, integers are whole numbers. In other words, they have no fractional component.
Two data types you can use to store an integer in Python are int and str. These types offer flexibility for working with integers in different circumstances. So, let’s get into it.
Step By Step Guide On How To Convert String To Int In Python :-
Converting one data type to another is also called “type casting” or “type conversion”.
Many languages offer built-in cast operators to do just that - to explicitly convert one type to another.
To convert, or cast, a string to an integer in Python, you use the int() built-in function.
currentYear = 2022 birthYearInput = input("Enter your birth year : ") print(type(birthYearInput)) birthYear = int(birthYearInput) print(type(birthYear)) # age = currentYear - birthYearInput age = currentYear - birthYear print(age)
- Using the int() function to convert a string datatype into an integer datatype is the best and simplest way to do it.
- In this example mentioned above, we are going to calculate a user’s age by getting input from the user and performing operations.
- In the first line, there is a variable named “currentYear” which has the value of the year 2022 as an integer datatype.
- In the next line, there is a variable called “birthYearInput” that will store the value that came from the input function. This value will be in the string datatype.
- In the next line, there is a print statement that has the type() function passed as the argument.
- The type() function returns the datatype of the value passed as the argument. In this case, it will display “<class 'str'>” in the output window.
- In the next line, there is a variable called “birthYear” which will hold the value returned from the int() function.
- The int() function will convert any value passed in as an argument into an integer datatype.
- In the next line, there is a print statement that will print the datatype of the variable “birthYear”. It will print “<class 'int'>” in the output window.
- In the next line, there is a comment that has an expression in it. The expression calculates the age of the user based on the input given by the user, subtracted from the current year, and stores it in the variable age.
- This line is a comment because this line will raise an error. In the output window, it will say “TypeError: unsupported operand type(s) for -: 'int' and 'str'”.
- This error occurs because of performing an operation on two different datatypes. The input from the user is in string datatype and the value of the variable “currentYear” is in integer datatype.
- In the next line, there is the same expression, but instead of using the direct input from the user, we are using the converted value from string to integer to perform this operation.
- In the next line, there is a print statement that will print the value of the variable age, calculated from the expression above.
Conclusion :-
So finally, in conclusion, we can say that with the help of this article, you can now convert any string value into an integer value in a Python program.
You just have to use the int() function to perform this task while programming.
I hope this tutorial on how to convert string to int in python helps you and the steps and method mentioned above are easy to follow and implement.