Skip to content

Convert string to int Python | Example code

  • by

Use the built-in int() function to convert string to int in Python. This function can take any type of data and converts it into an integer object.

This function takes two parameters: the initial string and the optional base to represent the data.

int(string)

Example convert string to int Python

Simple example code phrase string to integer in Python. Just pass the string to int() and it returns a decimal integer:

s = '100'

res = int(s)

print(type(res), res)

Output:

Convert string to int Python

Note: By default, int() assumes that the string argument represents a decimal integer. If you pass a hexadecimal string to int(), then it will throw a ValueError:

Do comment if you have any doubts or suggestions on this Python str to integer code.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading