×
Convert a Python String to Integer

If you are a programmer, one of the most important things to know about the conversions of data.

This is very useful when you receive data from an external source, it could be a number but in string format.

In that case, you can use a different conversion method to resolve your issue.

An integer is a whole number, positive or negative, without decimals, of unlimited length. You can use two data types int and str to store an integer in Python.

In Python, you can use int() function to convert a string to an integer.

There are several ways to convert a string to an integer in Python.

In this tutorial, we will show you how to convert a String to an Integer in Python with an example.

How to Represent Integer in Python

There are two data types to store an integer in Python, using either int or str.

For example, use a string literal to represent an integer as shown below:

s = "50"

The above example indicates that you want to store the integer 50 as a string.

You can represent the same thing using the integer data type as shown below:

i = "50"

There are several number systems including, binary and hexadecimal that uses different bases to represent an integer. You can also represent those number systems using the str and int data types.

A list of different bases to represent an integer is shown below:

  • b: binary (base 2)
  • o: octal (base 8)
  • d: decimal (base 10)
  • x: hexadecimal (base 16)

In the following example, we will store the decimal number 200 in hexadecimal and binary:

binary = 11001000
decimal = 200
hexadecimal_with_prefix = 0xC8

Convert a Python String to an int

You can use int() function to convert a string to an integer in Python. You can use int() function in any data type to converts it into an integer. You can also use the float() keyword to perform the same conversion.

In the following example, we will take a string object as an argument and returns an integer.


# Here age is a string object
age = "20"


# Converting a string to an integer
int_age = int(age)


print(age)
print(int_age)

Now, run the above code in the Python shell. You should see the following output:

As you can see, the output is similar. However, the first line is a string object while the second line is an integer object.

Let’s take another example to understand it better:

age = "20"
print(age + 10)

Now, run the above code in the Python shell. You should see the following error:

The above output clearly indicates that you will need to convert the age object to an integer before adding something to it. You can correct it with the following code:

age = "20"
age_int = int(age)
print(age_int + 10)

Now, run the above code in the Python shell. You should see the following output:

Convert a Python int to String

You can use str() function to convert an integer to a string in Python.

In the following example, we will take an integer object as an argument and return as a string.

# Here age is a string object
age = "20"
int_age = int(age)


print("Your age is: " + int_age)

Now, run the above code in the Python shell. You should get the following error:

This is because you cannot concatenate a string to an integer.

You will need to convert age to a string by using the str() as shown below:

# Here age is an integer object
age = "20"
int_age = int(age)


# Converting an integer to string
string = str(int_age)


print("Your age is: " + string)

Now, run the above code in the Python shell. You should get the following output:

Conclusion

In the above guide, we’ve learned how to convert a string to an integer in Python. We’ve also learned how to convert an integer back to a string. This is very useful if you want to store a value as an integer or perform mathematical operations on a value stored as a string.