How to write string literals in python without having to escape them?

kjakeb picture kjakeb · Jan 16, 2011 · Viewed 104.1k times · Source

Is there a way to declare a string variable in python such that everything inside of it is automatically escaped, or has its literal character value?

I'm not asking how to escape the quotes with slashes, that's obvious. What I'm asking for is a general purpose way for making everything in a string literal so that I don't have to manually go through and escape everything for very large strings. Anyone know of a solution? Thanks!

Answer

SilentGhost picture SilentGhost · Jan 16, 2011

Raw string literals:

>>> r'abc\dev\t'
'abc\\dev\\t'