All imo but in the beginning Python took Perl marketshare for scripting because it was simply less "esoteric". 10 Python programmers wrote similar programs to solve the same task whereas 10xPerl programmers would be more creative. All while coming with "batteries included" which helped to get stuff done quickly. And Python has always played well with C so you could optimize if need be.
At the same time, Python was a pretty early adopter for the server backend (and stuff like Zope existed) and also had great interopt with databases but PHP was a stong contender/#1. I'd say Rails timing was off by a bit or Ruby would have gotten more momentum (at that time there were already many things in Python land to compete like Django etc.). Meanwhile JS was still pretty cludgy to use and required a lot of hoops just to set up an environemnt and things like node came a bit late as well or JS would have taken more "web backend marketshare" from Python immediately. There was a window when the Python 2->3 transition slowed things down (imo) but overall it went slow but smooth for such a big transition.
Python was also an early player in other niches like security (both scripting and tooling).
And when ML happened to take off (grossly simplified) once again Python was there as an early player with good tooling and support for most things and with excellent ties to scientific computing.
It also got good support early from some key players, most noteably Google.
So basically, Python was always there when it mattered and "good enough", came with "batteries included" and the Python community has always been great + willing to quickly get working tooling and libraries in all relevant areas. Importantly Python has always been beginner friendly enough to attract a decent chunk of non programmer specialists from other fields that used it to dive into programming (scientific computing in general but biology comes to mind as a typical use case).