>>> a = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. \
Aenean vestibulum metus ut est suscipit, sed semper neque \
maximus. Sed gravida quam in odio porttitor viverra. Nulla \
pretium ante non ex porttitor, nec dictum odio porttitor."
>>> a.split()
['Lorem', 'ipsum', 'dolor', 'sit', 'amet,', 'consectetur', 'adipiscing', 'elit.', 'Aenean', 'vestibulum', 'metus', 'ut', 'est', 'suscipit,', 'sed', 'semper', 'neque', 'maximus.', 'Sed', 'gravida', 'quam', 'in', 'odio', 'porttitor', 'viverra.', 'Nulla', 'pretium', 'ante', 'non', 'ex', 'porttitor,', 'nec', 'dictum', 'odio', 'porttitor.']
>>> a = '''
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Aenean vestibulum metus ut est suscipit, sed semper neque
maximus. Sed gravida quam in odio porttitor viverra. Nulla
pretium ante non ex porttitor, nec dictum odio porttitor.
'''
>>> a.split()
['Lorem', 'ipsum', 'dolor', 'sit', 'amet,', 'consectetur', 'adipiscing', 'elit.', 'Aenean', 'vestibulum', 'metus', 'ut', 'est', 'suscipit,', 'sed', 'semper', 'neque', 'maximus.', 'Sed', 'gravida', 'quam', 'in', 'odio', 'porttitor', 'viverra.', 'Nulla', 'pretium', 'ante', 'non', 'ex', 'porttitor,', 'nec', 'dictum', 'odio', 'porttitor.']
>>>
No real surprise there as it looks like they both behave the same. Just be sure to always test the output, including the length. If it is supposed to be 326 but comes up as 327, then something is up, likely related to the newline after the leading triple quotes.
Bottom line, it is best to keep both methods in one’s tool box and simply be aware of both. If we’re in the middle of coding a long string but don’t want to roll off the screen then \
is there to let us do a hard return and continue typing. There will be no line breaks in the output. If we’ve got a chunk of prose with intentional line breaks then triple quotes is a no-brainer given we have preformatted text that simply needs to be enclosed in quotes (triple, albeit).
In bash ls
a directory and redirect to a .txt
file Then copy that content to the Python shell as a triple quoted string. This is when that extra element might show up.