EN
Python - parse string to int
0 points
In this article, we would like to show you how to parse the string to int in Python.
Quick solution:
xxxxxxxxxx
1
text = "10"
2
print(int(text)) # 10
or
xxxxxxxxxx
1
text = "10.99"
2
3
print(int(float(text))) # 10
4
print(int(text)) # ValueError
In this example, we present how to parse string to int in two different cases.
1. When we have int number inside the string
xxxxxxxxxx
1
text = "10"
2
print(int(text)) # 10
Output:
xxxxxxxxxx
1
10
2. When we have float number inside the string
xxxxxxxxxx
1
text1 = "10.01"
2
text2 = "10.99"
3
4
print(int(float(text1))) # 10
5
print(int(float(text2))) # 10
Output:
xxxxxxxxxx
1
10
2
10