Let's count the number of ways not to get $7$ heads in a row. We will put together atoms that consist of $0$ to $6$ heads followed by a tail. Any arrangement of heads and tails without $7$ heads in a row, appended with a tail, can be uniquely made up of a number of such atoms.
All arrangements of such atoms appear once somewhere in the sum
$$
\sum_{k=0}^\infty(x+x^2+x^3+x^4+x^5+x^6+x^7)^k
$$
where
$x$ represents $T$
$x^2$ represents $HT$
$x^3$ represents $HHT$
$\vdots$
$x^7$ represents $HHHHHHT$
For example, if we are looking for $HTTHHTTH$, append a $T$ and we get the term for $k=5$ where in the first factor, the $x^2$ ($HT$) was chosen, in the second factor, the $x$ ($T$) was chosen, then $x^3$ ($HHT$), then $x$ ($T$), then $x^2$ (HT), to get $HTTHHTTHT$. Note that the exponent of $x$ matches the number of tosses. To count the number of sequences of $40$ flips that do not contain $7$ consecutive heads, we look at the coefficient of $x^{41}$ in
$$
\begin{align}
\sum_{k=0}^\infty(x+x^2+x^3+x^4+x^5+x^6+x^7)^k
&=\frac1{1-x\frac{x^7-1}{x-1}}\\
&=\frac{1-x}{1-2x+x^8}
\end{align}
$$
The coefficient of $x^{41}$ is $955427104501$. There is a degree $8$ recursion to compute this without dividing polynomials: $c_n=2c_{n-1}-c_{n-8}$, where $c_n$ starts
$$
1,1,2,4,8,16,32,64,\dots
$$
The number of sequences of $40$ flips is $2^{40}$. Therefore, the probability of getting a sequence of $7$ heads in a row in $40$ flips is
$$
1-\frac{955427104501}{2^{40}}=0.131044110526
$$