In Python an strings can be converted into a integer using the built-in int() function. The int() function takes in any python data type and converts it into a integer.But use of the int() function is not the only way to do so. This type of conversion can also be done using thefloat() keyword, as a float value can be used to compute with integers.
Below is the list of possible ways to convert an integer to string in python:
1. Using int() function
Syntax: int(string)
Example:
num = '10' # check and print type num variable print(type(num)) # convert the num into string converted_num = int(num) # print type of converted_num print(type(converted_num)) # We can check by doing some mathematical operations print(converted_num + 20) |
As a side note, to convert to float, we can use float() in Python
num = '10.5' # check and print type num variable print(type(num)) # convert the num into string converted_num = float(num) # print type of converted_num print(type(converted_num)) # We can check by doing some mathematical operations print(converted_num + 20.5) |
2. Using float() function
We first convert to float, then convert float to integer. Obviously the above method is better (directly convert to integer)
Syntax: float(string)
Example:
a = '2'b = '3' # print the data type of a and b print(type(a)) print(type(b)) # convert a using float a = float(a) # convert b using int b = int(b) # sum both integers sum = a + b # as strings and integers can't be added # try testing the sum print(sum) |
Output:
class 'str' class 'str' 5.0
Note: float values are decimal values that can be used with integers for computation.
Recommended Posts:
- Convert integer to string in Python
- How to convert string to integer in Python?
- Different Ways to Convert Double to Integer in C#
- Python | Program to convert String to a List
- Python | Convert a list of characters into a string
- Python | Program to convert a tuple to a string
- Python program to convert a list to string
- Python | Convert string to DateTime and vice-versa
- Convert binary to string using Python
- Convert string to title case in Python
- Convert String to Float in Python
- Convert String to Long in Python
- Convert Object to String in Python
- Convert Decimal to String in Python
- Convert Python String to Float datatype
- Convert String to Set in Python
- Python program to concatenate two Integer values into one
- Check the equality of integer division and math.floor() of Regular division in Python
- How to take integer input in Python?
- Convert String to Double in Python3
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

