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.
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.
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 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.
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).
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.
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?
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.
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?