sec01 - Escape sequences

What are escape sequences?

An escape sequence is a string that can perform special control functions. For example, writing "\n" will cause a line break.

In this lecture, we will introduce some of the most commonly used escape sequences available in Python. For a complete list of all escape sequences in Python, please refer to the official documentation.

https://docs.python.org/3.13/reference/lexical_analysis.html#escape-sequences

Single/Double quote

In the previous lecture, we discussed that an error occurs when there is a single quotation mark inside a pair of single quotation marks, as in 'I'm Nico.'.

'I'm Nico.'  # error

In this case, one solution is to enclose both sides in double quotation marks.

"I'm Nico."  # If both sides are double quotation marks, there will be no error.

The above solution works fine, but there is a possibility that both single and double quotation marks may appear in the string, so it is also a good idea to remember how to use escape sequences. Write the code as follows.

'I\'m Nico.'  # By writing “\'”, both sides will not cause an error even if they are enclosed in single quotation marks.
Python interprets single quotation marks as the start and end of a string. However, \' is interpreted as the literal “single quotation marks.” Python interprets the code in the first line above as follows.
  1. '(The first single quotation mark) → Start point of a string (recognized as a symbol)
  2. I → The letter I
  3. \' → The letter '
  4. m → The letter m
  5. ....
Follow the above steps to process the string declaration statement, and interpret the string as continuing until the next single quotation mark indicating the end of the string appears. This prevents errors from occurring.

Now, let's check the execution results using an example of single quotation escape sequences.

greeting_by_nico = 'I\'m Nico. Nice to meet you.'
print(greeting_by_nico)

If the results are displayed as shown in the figure below, the execution is OK.

Double quotation marks can be used in the same way.

\" will be recognized as the letter ".

Linefeed (LF)

When written as \n, a line break is inserted when outputting a string with print, etc. The control code used to insert a line break is called “Linefeed.”

Execute the following code and check the results. (The linefeed escape sequence is written between I'm Nico. and Nice to meet you.)

greeting_by_nico = "I'm Nico.\nNice to meet you."
print(greeting_by_nico)

When you run the code, a line break will be inserted after I'm Nico., and Nice to meet you. will be output on a new line.

Horizontal Tab (TAB)

\t is recognized as a horizontal tab.

The tabs add just the right amount of space to make the code easier to read. (The actual display width varies depending on the editor and environment. Also, the appearance may be distorted depending on the length of the string to be displayed.)

Let's execute the following code and check the results. Each print statement is separated by tabs for each item.

print('Name\tAge\tCity')
print('Nico\t21\tTokyo')
print('Nancy\t9\tOsaka')

The output will look like the figure below. The left edges of the Age and City columns are aligned vertically. Tabs are used to insert spaces to align the left edges of each item, making the results easier to read.

There are other ways to write this as well.

name = 'Nico'
age = 21
print('Name:\t', name)
print('Age:\t', age)

When you execute the code and print the output, the variable values are aligned vertically as shown in the figure below, making them easier to read.

Backslash

\\ is recognized as a backslash (\) character.
\\ is used to represent Windows directories (folder structures) as follows.
'c:\Users\nico\Desktop'
In the above code, you can see that the color of \n in \nico has changed in Visual Studio Code. \n is an escape sequence that represents a line break.

As it is, the directory name nico will not be reflected correctly. To resolve this, write the code as follows.

'c:\\Users\\nico\\Desktop'
As shown in the code above, by writing \\, you can recognize the backslash as a character and prevent combinations such as \n from functioning as escape sequences.
About directory separator characters
In Windows, the backslash (\) is used as the directory separator, while in MacOS and Linux, the slash (/) is used. Each OS has its own designated directory separator, but in Python, paths can be written using a slash (/) on Mac and Linux, and either a backslash (\) or a slash (/) on Windows.

(※ In Windows, when you use slashes (/) to indicate paths, it will work in most cases, but depending on the environment, it may not work (or may not work as intended). Be sure to test it and confirm that it works as intended.)