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

前端 未结 6 1671
情歌与酒
情歌与酒 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 05:59

    (Assuming you are not required to input the string from directly within Python code)

    to get around the Issue Andrew Dalke pointed out, simply type the literal string into a text file and then use this;

    input_ = '/directory_of_text_file/your_text_file.txt' 
    input_open   = open(input_,'r+')
    input_string = input_open.read()
    
    print input_string
    

    This will print the literal text of whatever is in the text file, even if it is;

     '   ''' """  “ \
    

    Not fun or optimal, but can be useful, especially if you have 3 pages of code that would’ve needed character escaping.

提交回复
热议问题