Python How to convert String to int
By:Roy.LiuLast updated:2019-08-11
In Python, we can use int() to convert a String to int.
# String num1 = "88" # <class 'str'> print(type(num1)) # int num2 = int(num1) # <class 'int'> print(type(num2))
1. Example
A Python example to add two numbers.
1.1 Add two String directly.
num1 = "1" num2 = "2" num3 = num1 + num2 print(num3)
Output
12
1.2 Try it again with int()
num1 = "1" num2 = "2" # convert string to int num3 = int(num1) + int(num2) print(num3)
Output
References
From:一号门
COMMENTS