I want to create something like a dictionary for python code examples. My problem is, that I have to escape all the code examples. Also r'some string' is not useful. Would you recommend to use an other solution to query this entries?
import easygui
lex = {"dict": "woerter = {\"house\" : \"Haus\"}\nwoerter[\"house\"]",\
"for": "for x in range(0, 3):\n print \"We are on time %d\" % (x)",\
"while": "while expression:\n statement(s)"}
input_ = easygui.enterbox("Python-lex","")
output = lex[input_]
b = easygui.textbox("","",output)
Use triple quoting:
Triple-quoted strings (using
'''or"""delimiters) preserve newlines and any embedded single quotes do not need to be escaped.The
\escape after the opening'''triple quote escapes the newline at the start, making the value a little easier to read. The alternative would be to put the first line directly after the opening quotes.You can make these raw as well;
r'''\n'''would contain the literal characters\andn, but literal newlines still remain literal newlines. Triple-quoting works with double-quote characters too:"""This is a triple-quoted string too""". The only thing you'd have to escape is another triple quote in the same style; you only need to escape one quote character in that case: