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!

Is there any way to prevent the closing apostrophes from populating when opening the multi-line string?

3 Likes

I’m running into a problem. I keep trying to put in “”", but it does not allow me to. I can only put in “” or “”"""". There is a SyntaxError when I run either of these.

Assign the string here

to_you = ‘’’’’’
Stranger, if you passing
meet me and desire to
speak to me, why
should you not speak to me?
An why should I not speak
to you?’’’’’’
print(to_you)

I’m not sure why would you like to do that? It saves you time from writing them at the end.

The problem you have is with these:
’’’’’’
The look different from the normal ones:
“”"

Here’s the difference in CodeAcademy:

Please copy this and try it at your end:

# Assign the string here
to_you = """Stranger, 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)

It should work.

to_you = “”“Stranger, 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)

keep getting “Did you set to_you to the given Whitman poem?”

1 Like

The editor by default creates an empty doc_string,

''''''

The next thing to do is place the cursor in the middle and enter a newline (hard return).

'''
  Enter any block of multiline text (or hidden code)
  which will keep the formatting we give it.
'''

Note the term doc_string. That’s not exactly correct as this only applies when it is the first statement inside the function code block.

def foo(bar):
    '''
    a function to return the value of `bar`
    '''
    return bar

When we write this anywhere else in the source code it is strictly a comment unless assigned to a variable, in which case it becomes a multiline string, which also retains any formatting we give it.

We can access the doc_string by polling that attribute of the function.

>>> print (foo.__doc__)

    a function to return the value of `bar`
    
>>> 
1 Like
to_you ='''
Stranger, 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?
'''
p = "1"
  to_you += p
print(to_you)

Im trying to execute the code above to include 1 in text but its showing indentationerror

The second last line is indented. Unindent that line and it will work.

I am getting the same and can’t seem to see how my entry is different to the solutions - they seem to be written out the same for me? Did you manage to get it to run eventually?