Python Forum

Full Version: convert a string into an int in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How would I convert a string into an int in Python?
# this is string
mystring = '12345'

# convert to int
myint = int(mystring)
If a string contains only numerical characters, you can convert it into an integer using the int() function.

>>> int('227')
227

Let’s check the types:

>>> type('227')
<class ‘str’>

>>> type(int('227'))
<class ‘int’>