Skip to content
Home » String Literals with Single, Double and Triple Quotes

String Literals with Single, Double and Triple Quotes

Spread the love

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

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."

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

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 '

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

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.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

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:


Spread the love
Tags:

Leave a Reply