Free Python course. Sign Up for tracking progress →

Python: Escape sequences

Here's some dialog we want to show:

- Are you hungry?
- Aaaarrrgh!

Let's try to display a string with this text:

print("- Are you hungry?- Aaaarrrgh!")
# => - Are you hungry?- Aaaarrrgh!

As you can see, the result was not what we wanted. The lines are arranged one after the other, not one below the other. We need to somehow tell the interpreter to "press Enter" - to start a new line after the question mark. This can be done with the symbol \n:

print("- Are you hungry?\n- Aaaarrrgh!")
# => - Are you hungry?
# => - Aaaarrrgh!

\n — is an example of an escape sequence. These sequences are also called control constructions. They can't be displayed in the same way as how they're typed.

When you type text in Word, you press Enter at the end of a line. The editor puts a special invisible character at the end of the line called LINE FEED (LF). In some editors, you can actually display the invisible characters. Then the text will look something like this:

- Hey!¶
- Oh, hey!¶
- How's it going?

The device that outputs the corresponding text takes this character into account. For example, the printer drags the paper up one line when it encounters the LF, and the text editor drags all subsequent text below, also by one line.

There are several dozen such invisible characters, but in programming there are usually only a few. In addition to the line feed, there can also be:

  • tab \t — the gap that you get when you press Tab
  • carriage return \r — only works in Windows

You can recognize these control constructions in the text by the \. symbol. Programmers often use the line feed \n to properly format text. For example, if we write this code:

print("Gregor Clegane\nDunsen\nPolliver\nChiswyck")

This will appear on the screen:

Gregor Clegane
Dunsen
Polliver
Chiswyck

When working with the line feed symbol, consider the following points:

  1. It doesn't matter what comes before or after \n: a character or an empty string. The line feed will be detected and executed in any case

  2. A string can contain only \n and nothing else:

  print('Gregor Clegane') # String with text
  print("\n") # String with an invisible line feed character
  print('Dunsen') # String with text

The program will display this on the screen:

  Gregor Clegane


  Dunsen
  1. In the code, the escape sequence \n looks like two characters, but from the interpreter's perspective, it's just one special character

  2. If you want to output \n as text (two separate printable characters), you can use escaping - adding another \ at the beginning. The sequence \\n will be displayed as the characters \ and n, one after the other.

print("Joffrey loves using \\n")
# => Joffrey loves using \n

Windows uses \r\n by default to start a new line. This combination works well on Windows, but creates problems when transferring to other systems. For example, when there are Linux users in the development team.

The point is that the sequence \r\n has a different interpretation depending on the chosen encoding, which we is something we'll talk about later. For this reason, it's customary among developers to always use \n without \r.

In this case, the line feed is always treated the same and works fine in any system. Remember to configure your editor to use \n.

Instructions

Write a program that displays on this the screen:

- Did Joffrey agree?
- He did. He also said "I love using \n".

This program should have only one print(), but the result on the screen should look exactly like the one shown above.

The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.

Tips

Definitions

  • Escape sequence a special combination of characters in text. For example, \n — is a line feed.


If you got stuck and don't know what to do, you can ask a question in our community