What does the backslash in the variable declaration mean? (`=\`)

In this splitting string exercise, what is the given syntax in the string “= /”.

spring_storm_text =
“”"The sky has given over
its bitterness.

To my knowledge, we have not been exposed to this syntax. Is this an unmentioned escape sequence? I don’t see an end to the /" and based on the notes from the discussion forum it looks like there should be one if indeed that’s what this syntax is.

It is a backslash, not a forward slash:

spring_storm_text = \
"""The sky has given over 
its bitterness. 
Out of the dark change 
...
"""

… and it is read by the Python interpreter as a line split marker. Without it, the single, isolated line, spring_storm_text = would throw a syntax error.

In Python, there are two ways that you can split a line. One is by use of the backslash, as seen here, and the other is by means of parentheses, brackets or curly braces, within which any line can be split.

Why do they want to split this particular line? Probably so that the poem would look nice on-screen. Without the split, it would be:

spring_storm_text = """The sky has given over 
its bitterness. 
Out of the dark change 
...
"""

… not as pretty, you might agree.

It is unfortunate that this was used in a lesson dealing with escape sequences, for it is not an escape sequence marker. Escape sequences occur only within a string.

6 Likes

got it! Thanks

they throw some random stuff out every once and awhile.

3 Likes

Sorry, Could anyone tell me what is the role of this backward slash in this string?
spring_storm_text = \

2 Likes

I think the first backward slash in this exercise is ‘escaping’ the initial line break in the code between the declaration of the variable and the beginning of the multi-line string. Notice the code line numbers: the first line of the multi-line string is not just wrapped to the second line for visibility, it actually begins a new line of code. If you remove the backward slash, it throws a Syntax Error because it interprets the code as trying to declare a variable with no assigned value.

1 Like

Nope, I tried just deleting the \ and when I print it I got the same result.

Anyone have idea why is there =\ at all if you can just = “string you want to print”?

@system2494758475, I think you may be incorrectly interpreting my previous response. It matters which lines your code breaks on. If your code starts a new line between the variable declaration and the multi-line string without the backslash to escape the code line break, it should result in a syntax error.

This works…:

…and this works…:

…but this doesn’t work:

Now if your underlying question is ‘Why would you start your multi-line string on a separate line, instead of just starting it on the line with the variable declaration?’ then there are a number of different reasons why a coder might include that line break, but I think they all ultimately boil down to ‘it makes your code easier to read’.

3 Likes

That makes sense, thanks, I din’t think about readability of a code

One of the things that still get me off balance is semantics as it relates to string splitting. Take a multi-line string for example, it is already splitted by having each sentence in separate lines. After using .split('\n') on it, we have all the sentences come together in a “single line”. How in this world are we going to refer this to splitting? Obviously, i am missing something and i am sure splitting is the best word to use for the procedure. However, i need help in coming to terms with it for better and lasting comprehension

1 Like