public static String pythonEscape(String s){
  StringBuilder sb=new StringBuilder();
  for (int i=0; i < s.length(); i++) {
    char c=s.charAt(i);
switch (c) {
case '\n':
      sb.append("\\n");
    break;
case '\r':
  sb.append("\\r");
break;
case '\t':
sb.append("\\t");
break;
case '\\':
sb.append("\\\\");
break;
case '\"':
sb.append("\\\"");
break;
case '\'':
sb.append("\\\'");
break;
default :
sb.append(c);
}
}
return sb.toString();
}

public static String pythonEscape ( String s ) { StringBuilder sb = new StringBuilder ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { char c = s . charAt ( i ) ; switch ( c ) { case '\n' : sb . append ( "\\n" ) ; break ; case '\r' : sb . append ( "\\r" ) ; break ; case '\t' : sb . append ( "\\t" ) ; break ; case '\\' : sb . append ( "\\\\" ) ; break ; case '\"' : sb . append ( "\\\"" ) ; break ; case '\'' : sb . append ( "\\\'" ) ; break ; default : sb . append ( c ) ; } } return sb . toString ( ) ; }
we escape some characters in s to be able to make the string executable from a python string
