FAQ: Learn Python: Syntax - Multi-line Strings

This community-built FAQ covers the “Multi-line Strings” exercise from the lesson “Learn Python: Syntax”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Multi-line Strings

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

3 posts were split to a new topic: How to create a multiline string in the editor?

2 posts were split to a new topic: I cannot enter the required quotation marks?

2 posts were split to a new topic: How does my code differ from the solution?

2 posts were split to a new topic: Why do I get an indentation error?

2 posts were split to a new topic: Why do I need to learn the basics?

A post was split to a new topic: Can we perform the same operations on multi-line strings that we perform on other strings?

In this lesson about using “”" for multiline strings - i did the following:

Assign the string here

to_you = “”“if you passing meet me and desire to speak to me, why
should you not speak to me?
And why should I not speak to you?”""

print(to_you)

And I get a message on the bottom saying:

Did you set to_you to the given Whitman poem?

How do I set ‘to_you’ to the given Whitman poem if not the way I did it above?
I’d like to get this right and move on to the next lesson.
Thanks in advance for your helpl

It looks like you’re missing the first word, "Stranger," in the string.

Welcome to the forums!

1 Like

I noticed that later. Thanks, @victoria_dr !

1 Like

Why ever use single or double quotes over triple quotes?

Single and double quotes do the same thing, it usually comes to preference on which to use.

Triple quotes in the other hand help with formatting since any newlines you skip or make are reflected in the printed statements. SIngle and double quotes will give you sytax errors if you add new lines

We can add new lines by using the continuation operator, \.

Eg.

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

When using triple quotes we might not get the split we expect.

Eg.

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

Now split it. How many elements do you get? Are any of them empty or newlines?

1 Like

Ah I see, only started learning python a month ago.

Also, I was only thinking about it from a printing perspective for some reason.

I haven’t encountered the continuation operator before this so today I learned that, thanks :slight_smile:

To answer your question? when I split the first example and second example on a new line, the later behaves as expected and gives a few list items while the former just results in one element.

1 Like
>>> 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.

1 Like

MUTLILINE STRING TREATED AS A COMMENT

HI . Concerning the text
" If a multi-line string isn’t assigned a variable or used in an expression it is treated as a comment."

I understand that a detached string literal will not affect the program flow. Is this what the words “is treated as a comment” mean here? Or is there some more to “treating” it as a comment? I see for example that ordinary comments are displayed faded in the editor. But a detached multiline string is not displayed as faded.

Will be ignored by the parser.