Convert string to int Python | Example code

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

Simpl 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 an ValueError:

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

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.