Python string to int

Python string can be converted to int using int(strval). Note that it will throw ValueError for invalid string. Here is a quick example to convert string to int.

s = "7"
v = 2 + int(s)
print v
s = "Invalid int"
v = 2 + int(s)
9
Env: Python 2.7.6
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments