Today we’ll be talking about string literals.
We can use single, double or triple quotes for string literals. Here’s an example with double quotes:
String Literals in Single Quotes
We use single quotes if we need double quotes inside the string:
>>> print('He said: "Help me!"')
He said: "Help me!"
We can also use single quotes inside single quotes but then we must escape them with a backslash:
>>> text = 'I don\'t like Mike\'s dog.'
>>> text
"I don't like Mike's dog."
String Literals in Double Quotes
We use double quotes if we need single quotes or apostrophes inside the string:
>>> print("Jannet's car's much faster!")
Jannet's car's much faster!
We can also use double quotes inside double quotes but then we must escape them with a backslash:
>>> text = "He said: \"I don't know.\""
Here you can see the difference between echoing a string and printing it – watch the quotes in the output.
>>> text
'He said: "I don\'t know."'
>>> print(text)
He said: "I don't know."
Strings in Triple Quotes
We use triple quotes if the string must span more than one line. These may be triple double quotes:
>>> text = """
... This is a longer string
... that spans several lines.
... That's why we are using
... triple quotes.
... """
>>> print(text)
This is a longer string
that spans several lines.
That's why we are using
triple quotes.
or triple single quotes:
>>> text = '''
... This is a longer string
... that spans several lines.
... That's why we are using
... triple quotes.
... '''
>>> print(text)
This is a longer string
that spans several lines.
That's why we are using
triple quotes.
And this is what happens if we just echo the string: All the special characters are printed:
>>> text
"\nThis is a longer string\nthat spans several lines.\nThat's why we are using\ntriple quotes.\n"
Using triple quotes for strings that do not span multiple lines is also possible, however, not very common.
>>> text = """ just a sample """
>>> text
' just a sample '
>>> text = ''' just a sample '''
>>> text
' just a sample '
Inside triple quotes we can use both single and double quotes:
>>> text = """
... The woman said: "I don't like the dog
... because it always bites me
... whenever we meet."
... """
>>> print(text)
The woman said: "I don't like the dog
because it always bites me
whenever we meet."
If there are special characters inside the triple quotes, they work as they should. If we have a newline character, a newline is inserted. With a newline character at the end of the line, two newlines are inserted: one because it’s the end of the line anyway, the other because of the \n character. This happens after the word ‘special’ in the following example:
>>> text = """
... This text is to\n demonstrate how\n special\n
... characters work inside
... triple quotes.
... """
>>>
>>> print(text)
This text is to
demonstrate how
special
characters work inside
triple quotes.
We can use the \ character at the end of line within strings enclosed in triple quotes to prevent the end of line from taking effect:
>>> text = '''
... This is how \
... the newline character \
... gets ignored.
... '''
>>> print(text)
This is how the newline character gets ignored.
Here’s the video version of this article: