FAQ: String Methods - Splitting Strings III

This community-built FAQ covers the “Splitting Strings III” exercise from the lesson “String Methods”.

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

Computer Science

FAQs on the exercise Splitting Strings III

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: What does the backslash in the variable declaration mean? (=\)

in this exercise’s example:
smooth_chorus =
“”“And if you said, “This life ain’t good enough.”
I would give my world to lift you up
I could change my life to better suit your mood
Because you’re so smooth”""

chorus_lines = smooth_chorus.split(’\n’)

print(chorus_lines)
[‘And if you said, “This life ain’t good enough.”’, ‘I would give my world to lift you up’, ‘I could change my life to better suit your mood’, “Because you’re so smooth”]

I am confused why only the first in-string apostrophe (This life ain’t good enough) is escaped by python automatically with a backslash, but not the second one. ( Because you’re so smooth)

forum format will not show the backlash, but on the actual excercise page example, it shows on the first case but not on the second one.

Welcome to the forums!

Can you post the link to the exercise? You seem to have posted in the wrong exercise’s forum topic. Additionally, please format your code using the </> button so we can see exactly what you are talking about.

hi,Victoria
this is the link for my exercise:
https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-101/modules/cspath-python-strings/lessons/string-methods/exercises/splitting-strings-iii

smooth_chorus = \
"""And if you said, "This life ain't good enough."
I would give my world to lift you up
I could change my life to better suit your mood
Because you're so smooth"""
 
chorus_lines = smooth_chorus.split('\n')
 
print(chorus_lines)
['And if you said, "This life ain\'t good enough."', 'I would give my world to lift you up', 'I could change my life to better suit your mood', "Because you're so smooth"]

the code i presented is the example code in this lesson. I am just confused with these two parts in the output of the last print command

This life ain\'t

Because you're

in the first case, Python automatically escaped the ’ character inside ain\'t, where as there is none for inside you're

The latter case is written in triple quoted string so the apostrophe does not need to be escaped. The former is written in a single quoted string, so does need to be escaped.

2 Likes

Thank you for the clarification!

1 Like

I got stuck with “”" , both in the beginning of the code and in the end.

I think I never come across it before so,

  1. What is it?
  2. Where to find its detail in cheat sheet?
  3. Do we encounter it before we get to this exercise? Why I seem the only one who don’t know what it is? ( Apart from \ , of course )

I have no idea if it’s introduced before that point, it might be worth some feedback to cc if it’s not been covered at all in the course until that point. You can find a little info on this kind of string in the docs- 3. An Informal Introduction to Python — Python 3.9.6 documentation (or a web search would net you more info).

It’s just another way of writing string literals with the biggest difference being that you can easily include new lines as part of the string (without the \n escape) and a secondary difference being you can include both " and ' characters inside the string without escapes (but not three of the original character used).

1 Like

The triple quote (either single or double quote mark) denotes a multi-line string object. It can be used to insert documentation when written as standalone (without assignment). When it is assigned it is treated as a str object.

Above, it is assigned, so represents an object of data which can be operated on with any of the available methods inherited from the str class.

What may not come up in this introductory course is the usage as a docstring at the head of a function.

def foo(bar):
    """
    A function to foo any bar.
    """

When written at the head of a function body, Python can retrieve it with the help() function.

>>> print (help(foo))
    A function to foo any bar.
>>>

As for the \, that is a continuation operator, much like a newline.

1 Like

thanks you, Im a big fan of your replied-solutions @mtf"

2 Likes

Thanks. That help a lot. Cheers

hey, i was just wondering what the first ‘’ does.

Do you mean what the first \ does? It is an escape character used to tell the parser that the character following is intended as printable. Notice in your post it didn’t print. That’s because it is a special character, that itself needs to be escaped if it is to render.

‘\’ is written as, '\\'.

When we are using single quotes on a string, any apostrophes in that string will need to be escaped or the parser will think that the string as ended.

‘Don’t be fooled by crocodile tears.’ is written as, 'Don\'t be fooled by crocodile tears.'

Quick question on the example in the Splitting Strings III lesson. The output from the chorus_lines list shows double quotes surrounding the last item whereas all the other items in the list are surrounded by single quotes. Is this just a typo?

1 Like

An easy one to make, and quite likely. Given there is no significant difference in their behavior it shouldn’t matter, but it is good that you spotted that. One of our key goals from the outset is consistency which only comes from forming habits, some of them fairly rigid.

1 Like

Hi there,

I’m not following your explanation as to why the “ain’t” word has a backslash in the output. The entire chorus string is enclosed between two sets of 3 quotation marks, including the quote that includes ‘ain’t’, so shouldn’t python just pass it through without additional formatting added?

When we split a triple quoted string, the list consists of single quoted strings, or double quoted strings that contain apostrophes.

So how could we format the code to still split the string, but avoid the inserted escape character?