Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Converting String to int

00:01 Converting a Python string to an int. If you have a decimal integer represented as a string, and you want to convert the Python string to an int, then you can just pass the string to int(), which returns a decimal integer.

00:20 You can see the variable s has been defined as the string "110". Returning that shows that it’s a string with the quotes. And if we want the integer version of that, we can see that that’s returned as an integer without any quotes around it.

00:38 We can check the type of that output using type(). As you can see, it’s of <class 'int'>. By default, int() assumes that the string argument represents a decimal integer.

00:52 If you pass the wrong kind of data to it, this can lead to some confusion if you’ve not been fully aware of the types of numbers you’re passing to it.

01:05 As you can see, trying to pass it a hexadecimal number here has generated a ValueError. The error message says the string is not a valid decimal integer.

01:16 It’s important to recognize the difference between two types of failed results of passing a string to int(). Firstly, let’s look at a logical error where int() does know how to parse the string, but not in the way we expected.

01:32 Here’s a string that’s intended to be binary, but int() will misinterpret it. In this example, the intended result was 210, the decimal representation of that binary string.

01:46 Unfortunately because that behavior wasn’t specified int() will assume the string was a decimal integer and produce the results seen on screen.

01:54 A good safeguard for this behavior is to always define any string representation using explicit bases.

02:07 However, passing our new binary string to int() leaves the ValueError we saw before. If you pass a string to int() which is not in decimal, you can specify the number using the “base” argument as seen here.

02:25 Finally, the correct result is obtained. int() has understood you’re passing a binary string and expecting a decimal integer. Note that the value for the base argument is not limited to 2, 8, 10, and 16, the number of bases we’ve seen so far, but can be any arbitrary number.

02:47 Now that you’re comfortable with the ins and outs of converting Python strings to ints you’ll learn how to do the inverse operation in the next section.

javierortin on May 19, 2020

Would be nice if the tutorial went deeper and added signed integers. That’d be useful.

ChuckB on June 2, 2023

To better clafify what is meant by an int, you could show that an int can participate in arithmatic computations while a str can not. You might show 2 times an int and 2 times a str.

Become a Member to join the conversation.