Hat difference between "" and '' in string definition

Peter Otten __peter__ at web.de
Sat Sep 9 02:58:52 EDT 2017


Andrej Viktorovich wrote:

> What is difference between string definitions:
> s="aaa"
> and
> s='bbb'

There's no difference. It helps you avoid explicit escapes, i. e.

>>> "What's up?"
"What's up?"

is a tad more readable than

>>> 'What\'s up'
"What's up"

Likewise, multiline strings are easier to read when written as 

>>> """foo
... bar
... baz
... """
'foo\nbar\nbaz\n'

than

>>> "foo\nbar\nbaz\n"
'foo\nbar\nbaz\n'

If you do not want to mess up indentation implicit string concatenation is 
sometimes useful. With that

>>> if True:
...     """foo
... bar
... baz
... """
... 
'foo\nbar\nbaz\n'

may be rewritten as

>>> if True:
...     (
...         "foo\n"
...         "bar\n"
...         "baz\n"
...     )
... 
'foo\nbar\nbaz\n'





More information about the Python-list mailing list