This probably plenty fast enough, but a brute force method strikes me as a very inefficient approach. I can think of a way that should be faster and more efficient, and avoids you constructing a million-character string.
Here are some suggestions/hints:
You calculate the same constant
string each time, despite the fact that it’s always going to be the same. Do you really need to construct it every time? (Or at all?)
For every value of k
you add to constant
, you know how long it is. Do you actually need to add it to the string if you’re not going to look at it? Or could you just decrement that from the number of digits left before you get to the digit you want?
How many one-digit numbers are in the string? And two-digit numbers? And so on.
My actual solution in spoiler text:
We can show that there are always \$ 9 \times 10^{n-1} \$ numbers which have \$n\$ digits, so by computing the sum
\$ 9 + 90 + 900 + \dotsb \$
until we go over the target value, we can work out how many digits the number we land in has (say \$ N \$). You can then jump back to the start of the \$ N \$-digit numbers and count up from there. You don't even need to be constructing the string; you can just use lengths to work out where we end up.
Oh, and one style comment:
Quoting from PEP 8 on imports:
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
Don’t put them inside your product
definition. The way that Python caches module imports means that it won’t be a performance hit, but it does make it harder to find your module dependencies in a large file. I realise that this isn’t much of an issue in short snippets like this, but it’s a good habit to put your import statements in the standard location.