I understand that on string assignment that exceeds 1 line you can use either backslash, parenthesis or triple quotes. Is there any practical difference? Which is considered a better coding practice?
For example:
STR1 = """Peanut
butter
jam"""
STR2 = "Peanut" \
"butter" \
"jam"
STR3 = ("Peanut"
"butter"
"jam")
All of which run perfectly fine, but which one is less prone to future bugs, or is a better practice?
STR1is, as pointed out in snatchysquid's answer, actually a different string toSTR2andSTR3. This may not be relevant, depending on the case (e.g. when using regular expressions you can turn on the verbose flag to ignore the extra whitespace).Between
STR2andSTR3, the guidance in PEP8 suggests the latter:With the backslashes, you also can't have comments: