EN
Python - convert string to integer
0 points
In this article, we would like to show you how to convert string to integer in Python.
Quick solution:
xxxxxxxxxx
1
number = "123"
2
result = int(number)
3
4
print(type(result)) # <class 'int'>
In this example, we simply use int()
to convert the string to the integer.
xxxxxxxxxx
1
number = "123"
2
result = int(number)
3
4
print(result) # 123
5
print(type(result)) # <class 'int'>
Output:
xxxxxxxxxx
1
123
2
<class 'int'>
In this example, we use decimal.Decimal()
to a float number. This solution doesn't round the number with more than 15 decimal places like float()
.
xxxxxxxxxx
1
from decimal import Decimal
2
3
number = "1.1111111111111111"
4
result = Decimal(number)
5
6
print(result) # 1.1111111111111111
7
print(type(result)) # <class 'decimal.Decimal'>
Output:
xxxxxxxxxx
1
1.1111111111111111
2
<class 'decimal.Decimal'>