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

前端 未结 6 1655
情歌与酒
情歌与酒 2020-11-27 05:18

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

6条回答
  •  抹茶落季
    2020-11-27 06:07

    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.

提交回复
热议问题