You can use the escape function found in the xml module
from xml.sax.saxutils import escape
my_string = "Some string with an &"
# If the string contains &, <, or > they will be converted.
print(escape(my_string))
# Above will return: Some string with an &
Reference: Escaping strings for use in XML