How to take the nth digit of a number in python
<p>I want to take the nth digit from an N digit number in python.  For example:</p>

<pre><code>number = 9876543210
i = 4
number[i] # should return 6
</code></pre>

<p>How can I do something like that in python? Should I change it to string first and then change it to int for the calculation?</p>

<p>You can do it with integer division and remainder methods</p>

<pre><code>def get_digit(number, n):
    return number // 10**n % 10

get_digit(987654321, 0)
# 1

get_digit(987654321, 5)
# 6
</code></pre>

<p>The <code>//</code> does integer division by a power of ten to move the digit to the ones position, then the <code>%</code> gets the remainder after division by 10.  Note that the numbering in this scheme uses zero-indexing and starts from the right side of the number.  </p>

python int