How to parse a string to a float or int in Python?

I’m working on a Python task where I have a string input and I need to convert it to a float or int. The string input may contain numbers and other characters. Can anyone suggest a way to do this using Python?

Here’s the code I’ve tried so far:

This code works for simple cases, but it may raise a ValueError if the string input contains non-numeric characters. Can anyone suggest a more robust way to parse a string to a float or int? I’m open to any suggestions or advice. Thank you in advance for your help!