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.

39 Likes

got it! Thanks

they throw some random stuff out every once and awhile.

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

2 Likes

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

10 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

The sentences don’t come together in a single line as you suggest. Using the .split() method creates a list of strings. The strings in the list are formed by splitting the original string on whatever character is chosen. In the context of this example that would be the newline character '\n'.

1 Like

when we split by \n we have no \n in string how we could say split by \n then ?

If you look at the variable chorus_lines, basically it takes the text in smooth_chorus and breaks every line of text into a string. The .split(’\n’) method attached to chorus line is what asks the computer to do that, as \n mean new line.
Once the computer has all the lines of smooth_chorus into strings, it puts it back into a list in chorus_lines.

There is an \n in the string, it’s just invisible. It’s a special invisible character that tells the program displaying your text that there’s a line ending here, and to display the next character in your text on the next line. This special character is secretly inserted at the cursor position when you press enter on your keyboard.

Most text editors won’t show you this special character, unless you explicitly tell them to, if they have that option, but when dealing with code it’s essential to know of its existence because it’s a part of multi-line strings.

1 Like

I think \ is used outside the string for newline and we can’t write after it in the same line. While \n must be used within the strings only for starting a newline after it. Am i correct?

\n is used within strings and is a newline character. \ is used to continue onto the next line doesn’t necessarily have to be used inside strings. This article explains it well.

This aptly clarifies it