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

Question

Asked By – kjakeb

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!

Now we will see solution for issue: How to write string literals in python without having to escape them?


Answer

Raw string literals:

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

This question is answered By – SilentGhost

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0