In this article, we will discuss how to convert a number in a string to an int object.

Table of Contents

Python provides a function int(), to convert a number in a string to an integer.

Syntax of int() function

int(str, base=10)

Parameters:

  • str: The string that contains the number like ‘23’ or ‘1234’ etc.
  • base: The base that will be used while converting the string to int. Valid values are from 2 to 36.
  • The default value is 10.

Returns:

  • It converts the given string to an integer and returns the int object.
  • If the provided string contains anything other than a number, then it will raise ValueError

Let’s see some examples where we will use int() function to convert string to an int object.

Convert string to int in python

Suppose we have a string as Str object and contains ‘234’. To convert this to an integer, i.e., int object, we will pass the string to int() function. Which converts this string to an integer and returns the int object. For example,

value = '234'

# Convert string to integer
num = int(value)

print(num)

print('Type of the object:')
print(type(num))

Output:

234
Type of the object:
<class 'int'>

As we did not provide the base argument, so by default, the base value 10 was used while converting the string to an integer.
But what if we want to convert a string with different base values to an integer?

Convert hex string to int in python

Suppose we have a string ‘0xFF11’ as Str object. It contains the hexadecimal representation of a number. To convert this to an integer, i.e., int object, we will pass this string to the int() function along with base value 16. Then the int() function will convert the hex string to an integer and returns the int object. For example,

value = '0xFF11'

# Convert hex string to integer
num = int(value, base=16)

print(num)
print(type(num))

Output:

65297
<class 'int'>

If we do not provide the base value as 16 with hex string in the int() function, then it will raise ValueError. So, always remember to provide base value as 16 while converting hexadecimal string to int object.

Convert binary string to int

Suppose we have a string ‘01110011’ as Str object. It contains the binary representation of a number. To convert this to an integer, i.e., int object, we will pass this string to int() function along with base value 2. Then the int() function will convert the binary string to an integer and returns the int object.

For example,

value = '01110011'

num = int(value, base=2)

print(num)
print(type(num))

Output:

115
<class 'int'>

Convert number string with comma to int

Suppose we have a string ’23,110’, it contains the numbers but also has some extra non-numeric characters, like commas. To convert this kind of string to an integer object is a little tricky. If we directly pass this to the int() function, then it will raise an error. For example,

value = '23,110'

num = int(value)

Output:

ValueError: invalid literal for int() with base 10: '23,110'

As string had characters other than digits, so int() raised an error. So, we need to remove all the extra commas from the string before passing it to the int() function. For example,

value = '23,110'

# Convert number string with comma to integer object
num = int(value.replace(',',''))

print(num)

Output:

<class 'int'>
23110

Similarly, we can avoid the error by removing other non-numeric characters from the string before passing it to the int() function.

Summary:

We can convert a number in string object to an integer using the int() function. This number string can be in binary, octal, or hexadecimal format.

Join a list of 2000+ Programmers for latest Tips & Tutorials