Solution Review: String to Integer
This lesson contains the solution review to convert a string into an integer in Python.
We'll cover the following...
We'll cover the following...
In this lesson, we will review the solution to the challenge in the last lesson. The problem is as follows:
Given some numeric string as input, convert the string to an integer.
You already know that we are not allowed to use the built-in int() function. Now to convert a string into an integer, we can make use of the following arithmetic:
As you can see, by multiplying each digit with the appropriate base power, we can get the original integer back. Let’s implement this in Python.
Implementation
Explanation
On line 3, output_int is initialized to 0. The code on lines 5-10 ...