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

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


Raw string literals:

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


If you're dealing with very large strings, specifically multiline strings, be aware of the triple-quote syntax:

a = r"""This is a multiline stringwith more than one linein the source code."""


There is no such thing. It looks like you want something like "here documents" in Perl and the shells, but Python doesn't have that.

Using raw strings or multiline strings only means that there are fewer things to worry about. If you use a raw string then you still have to work around a terminal "\" and with any string solution you'll have to worry about the closing ", ', ''' or """ if it is included in your data.

That is, there's no way to have the string

 '   ''' """  " \

properly stored in any Python string literal without internal escaping of some sort.