Python string quoting

If you were looking for a python equivalent of strconv.Quote or using json.dumps(str) to quote strings, then you are a clod - such as me. Another reminder that looking at the documentation of the standard library is very useful.

It never occurred to me that codecs (str.encode) are used not only for converting between text encodings like utf-8 ↔️ cp1251, but also for all sorts of formats like base64, bzip (sic!) and uu. In particular, the set of standard codecs includes 'unicode_escape', which escapes all characters that make a string unrepresentable as a Python literal in source code.

The only underwater rake - quotes are not taken into account, so the resulting string needs to be post-processed. But in general, this is a much less hacky (and probably faster) way to do "secured strings" than those mentioned at the beginning.

import codecs

# Convert the operand using uuencode.
codecs.encode(b'Alice\'s Adventures in Wonderland', 'uu')
#> b"begin 666 \n@06QI8V4G b'x\x9cs\xcc\xc9LNU/VpL)K\xcd+)-J-V\xc8\xccS\x08\xcf\xcfKI-\xcaI\xccK\x01\x00\xbe\x1f\x0b\xdf'

# the Caesar-cypher encryption of the operand
codecs.encode('Alice\'s Adventures in Wonderland', 'rot13')
#> "Nyvpr'f Nqiragherf va Jbaqreynaq"

Links

=> home
=> Python strings standart encodings

Proxy Information
Original URL
gemini://kotobank.ch/~merlin/python_quote.gmi
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
278.557467 milliseconds
Gemini-to-HTML Time
0.313759 milliseconds

This content has been proxied by September (ba2dc).