Skip to Content

Convert String to Integer in Python

In this tutorial, we will look at how to convert a string in Python to an integer, int datatype with the help of some examples.

How to convert string to int in Python?

convert string to integer in python

You can use the Python built-in int() function to convert a string to an integer in Python. The following is the syntax –

# convert string s to int
int(s)

It returns the passed string as an integer. Note that if the passed string does not represent a whole number, it will raise an error (even with float numbers represented as string).

Examples

Let’s look at some examples of using the above syntax to convert a string to an integer.

First, let’s apply the int() function on a string containing a whole number, for example, “150”.

# string storing a number
s = "150"
# convert string to int
num = int(s)
# display integer and its type
print(num)
print(type(num))

Output:

150
<class 'int'>

You can see that we get 150 as an integer.

What happens if you pass a floating-point number as a string to the int() function? Let’s find out.

# string storing a floating point number
s = "150.75"
# convert string to int
num = int(s)
# display integer and its type
print(num)
print(type(num))

Output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [2], in <module>
      2 s = "150.75"
      3 # convert string to int
----> 4 num = int(s)
      5 # display integer and its type
      6 print(num)

ValueError: invalid literal for int() with base 10: '150.75'

Here we pass “150.75” to the int() function. Since the string passed is not a whole number, we get an error. If you want to convert the above string to a numeric form in Python, use the float() function instead.

# string storing a floating point number
s = "150.75"
# convert string to float
num = float(s)
# display number and its type
print(num)
print(type(num))

Output:

150.75
<class 'float'>

We get the string as a float value.

What happens if you pass a non-numeric string to the int() function? Let’s find out.

# string with text
s = "cat"
# convert string to int
num = int(s)
# display number and its type
print(num)
print(type(num))

Output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [4], in <module>
      2 s = "cat"
      3 # convert string to int
----> 4 num = int(s)
      5 # display number and its type
      6 print(num)

ValueError: invalid literal for int() with base 10: 'cat'

As expected, we get an error because the passed string cannot be represented as an integer.

You might also be interested in –

  1. Extract Numbers From String in Python
  2. Python – Check if String Contains Only Numbers
  3. Python Tuple to Integer


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.