How can I sort a list of strings numerically in Python?

I’m trying to sort a list of strings numerically using Python’s built-in sort function. However, the sorting is not working as expected since the numbers are being sorted alphabetically instead of numerically.

Here’s an example list: my_list = [‘file1.txt’, ‘file12.txt’, ‘file3.txt’, ‘file2.txt’]

When I try to sort it using my_list.sort() , the output is ['file1.txt', 'file12.txt', 'file2.txt', 'file3.txt'] . However, I want the output to be ['file1.txt', 'file2.txt', 'file3.txt', 'file12.txt'] .

Here’s what I am doing:

Can anyone help me sort this list numerically using Python’s sort function or suggest any other method to achieve the desired result? Any help or suggestions would be greatly appreciated. Thank you!"