Escaping a string for use in a MySQL query in Python can be done using the MySQLdb.escape_string() function. This function takes a string as an argument and returns an escaped version of the string, ...
This package only supports python 3.6 and up. It may work on older versions (maybe even python 2) but I'm not sure.