[Question]
  [
## Background
Python 3 has many types of string literals. For example, the string `this 'is' an exa\\m/ple` can be represented as:
```
'this \'is\' an exa\\\\m/ple'
"this 'is' an exa\\\\m/ple"
r"this 'is' an exa\\m/ple"
'''this 'is' an exa\\\\m/ple'''
"""this 'is' an exa\\\\m/ple"""
r'''this 'is' an exa\\m/ple'''
r"""this 'is' an exa\\m/ple"""
```
As you can see, using different delimiters for strings can lengthen or shorten strings by changing the escaping needed for certain characters. Some delimiters can't be used for all strings: `r'` is missing above (see later for explanation). Knowing your strings is very useful in code golf.
One can also combine multiple string literals into one:
```
'this \'is\' an ''''exa\\\\m/ple'''
"this 'is' an "r'exa\\m/ple'
```
---
## Challenge
The challenge is, given a printable ASCII string, to output its *shortest* literal representation in Python.
### Details on string mechanics
Strings can be delimited using `'`, `"`, `'''` and `"""`. A string ends when the starting delimiter is hit again unescaped.
If a string literal starts with `'''` or `"""` it is consumed as the delimiter. Otherwise `'` or `"` is used.
Characters can be escaped by placing a `\` before them. This inserts the character in the string and eliminates any special meaning it may have. For example, in `'a \' b'` the middle `'` is escaped and thus doesn't end the literal, and the resulting string is `a ' b`.
Optionally, one of `r` or `R` may be inserted before the starting delimiter. If this is done, the escaping `\` will appear in the result. For example, `r'a \' b'` evaluates to `a \' b`. This is why `a ' b` cannot be delimited by `r'`.
To escape `'''` or `"""`, one only needs to escape one of the characters.
These literals can be concatenated together, which concatenates their contents.
### Rules
* The input is the string to golf. Printable ASCII only, so no newlines or other special characters.
* The output is the golfed string literal. If there are multiple solutions, output one.
* To simplify the challenge, in non-`r` strings any escapes except for `\\`, `\'` and `\"` are considered invalid. They must not be used in the output, even though `'\m'` is equal to `'\\m'` in Python. This removes the need to process special escape codes such as `\n`.
* Builtins for golfing Python strings are disallowed. Python's `repr` is allowed, since it's crappy anyway.
* Standard [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") rules apply.
## Example inputs/outputs
I tried my best to verify these, but let me know if there are mistakes. If there are multiple valid outputs to the cases, they are all listed below the input.
```
test
 -> 'test'
 -> "test"
te\st
 -> 'te\\st'
 -> "te\\st"
 -> r'te\st'
 -> r"te\st"
te'st
 -> "te'st"
te"st
 -> 'te"st'
t"e"s't
 -> 't"e"s\'t'
te\'st
 -> "te\\'st"
 -> r'te\'st'
 -> r"te\'st"
te\'\"st
 -> r'te\'\"st'
 -> r"te\'\"st"
t"'e"'s"'t"'s"'t"'r"'i"'n"'g
 -> """t"'e"'s"'t"'s"'t"'r"'i"'n"'g"""
 -> '''t"'e"'s"'t"'s"'t"'r"'i"'n"'g'''
t"\e"\s"\t"\s'\t"\r"\i"\n"\g
 -> r"""t"\e"\s"\t"\s'\t"\r"\i"\n"\g"""
 -> r'''t"\e"\s"\t"\s'\t"\r"\i"\n"\g'''
t"""e"""s"""'''t'''s'''"""t"""r"""'''i'''n'''g
 -> 't"""e"""s"""'"'''t'''s'''"'"""t"""r"""'"'''i'''n'''g"
t\"""e\"""s\"""'''t'''s'''\"""t\"""r\"""'''i'''n'''g
 -> r"""t\"""e\"""s\"""'''t'''s'''\"""t\"""r\"""'''i'''n'''g"""
t"e"s"t"s"t"r"i"n"g"\'\'\'\'\'\'\'\
 -> r't"e"s"t"s"t"r"i"n"g"\'\'\'\'\'\'\'''\\'
 -> r't"e"s"t"s"t"r"i"n"g"\'\'\'\'\'\'\''"\\"
"""t"'e"'s"'t"'s"'t"'r"'i"'n"'g'''
 -> """\"""t"'e"'s"'t"'s"'t"'r"'i"'n"'g'''"""
 -> '''"""t"'e"'s"'t"'s"'t"'r"'i"'n"'g''\''''
```
Thanks to [Anders Kaseorg](https://codegolf.stackexchange.com/a/132192/30164) for these additional cases:
```
\\'"\\'\
 -> "\\\\'\"\\\\'\\"
''"""''"""''
 -> '''''"""''"""'\''''
```
  
[Answer]
# [Python 3](https://docs.python.org/3/), ~~264~~ 262 bytes
```
f=lambda s,b='\\',r=str.replace:min(sum([['r'+d+s+d,d+r(r(s[:-1],b,b+b),d,d[1:]+b+d[0])+b*(s[-1:]in[b,d[0]])+s[-1:]+d][d in r(r(s+d[1:],b+b,'x'),b+d[0],b)or r(s,b+b,'')[-1:]==b:]for d in["'",'"',"'''",'"""']],[f(s[:k])+f(s[k:])for k in range(1,len(s))]),key=len)
```
[Try it online!](https://tio.run/##hVLdrtsgDL7PUyDfOAx6tGp3kbJH2AsAmkJDeqKmpAJ6dM7TdzZJt96NyMb48@cfyO2rvK/xx@Mx9ctw9eMgsvY9Wos69bmktxRuy3AK3XWObb5fW2MwoRpVVqMeVWpTm013ODrttVdeavKaY@eUV6P57qTy3yjgQJ45Gq/ZR87No0ZnRjFHUbOoyuMkGj9R6i2B9nJNFJA3AGVl9r3v3EQA0w0gaATUgFgtAHROm4k7u1A1Ni6dk0y41HpDPIf2qJdAM0nppL6Er55O8jFfb2sqYrrHU1nXJTdT/9d@W9L992k4vYf21xqDbCfZcM7MOQ2WkAvqhnZrqwUlYC5QXbBjQJbFHaVbrnhiDtothtovgAEwA4XvOgHOgBHwTDARapANYDNYMjKyTmBnsBHsFkQ8oHIAmW@EL6eQZL4krHyA9ERmkkhyfia3zGWVWb1w7RNNO/CPuhelCSk5S4IZIpy5fRrv5YP6g/Go9j/jcrecFywtKr5tlivhhm3K8sl1jaB1S3Ms9LAvBxSHnwKFEhM/eAWGnAO9dPgYlrZ6Rd@L3OzxIz0wyscf "Python 3 – Try It Online")
This works but is very slow without memoization, which you can add with
```
import functools
f=functools.lru_cache(None)(f)
```
It found an improved solution for one of the test cases:
```
t"e"s"t"s"t"r"i"n"g"\'\'\'\'\'\'\'\
 -> 't"e"s"t"s"t"r"i"n"g"'r"\'\'\'\'\'\'\'"'\\'
 -> r't"e"s"t"s"t"r"i"n"g"\'\'\'\'\'\'\'''\\'
```
Previous versions of this answer returned incorrect results on the following, which could be added as test cases:
```
\\'"\\'\
 -> "\\\\'\"\\\\'\\"
''"""''"""''
 -> '''''"""''"""'\''''
```
]