
In this lecture, you will learn about raw string literals, triple-quotes, and other methods for defining strings.
Table of Contents(目次)
raw string
A raw string is a string that is interpreted as written without escape processing.
Definition of raw string
Using the directory notation learned in the previous lecture as an example, we will introduce how to describe it using raw strings.
desktop_path = 'c:\\Users\\nico\\Desktop'
desktop_path_raw = r'c:\Users\nico\Desktop'

In the notation used in the first line, you must enter two backslashes each time. This can be particularly troublesome when representing directories as strings, as you must enter two backslashes at every path separator. Additionally, there are disadvantages, such as not being able to use directory paths copied from Windows Explorer as they are. (The separator character for copied directory paths is a single backslash, so you must replace it with two backslashes.)
The second line is a raw string, but when defining raw strings, addr
before the quotation marks to write them as r'...'
. (Capital R
also works.) When defined as a raw string, Python understands that it is a string to be treated as written, and treats \
as a character, so you can write it with a single backslash without worrying about escape sequences.
In Visual Studio Code, when a string is recognized as a raw string, the character color turns red. In the above figure, some characters are colored yellow, but you can ignore them. (Strictly speaking, the color of the parts related to regular expressions has changed, but since learning regular expressions is still a long way off, it is enough to remember that you can write without worrying about escape processing.)
Now, let's execute the above code. The strings assigned to each line will be output as shown in the figure below.

You can confirm that the path strings in each line are stored without escape processing such as line breaks.
How raw strings work
Let's check how raw strings work. There is a function called “repr” that can be used to display the string information held by a variable in its raw form, so let's use that.
Let's execute the following code.
desktop_path_raw = r'c:\Users\nico\Desktop'
print(repr(desktop_path_raw)) # String data is output in its raw state with “repr”.
When you execute this, you should see a result like the one shown in the figure below.

When the raw string is declared in the first line, you can see that another backslash is automatically added to the backslash. In other words, when you define a string using the r'...'
notation, Python converts it to the same data as a regular string and assigns it to the variable. This saves the programmer from having to write two backslashes.
Triple-quotes
There is a block of string that spans multiple lines, as shown below.
I am Nico.
I am Nancy.
I am Noah.
If you want to define this as a string including line breaks, you can use line break escape sequences and write it as follows.
introductions = 'I am Nico.\nI am Nancy.\nI am Noah.'
This method can also be used, but it may not be easy when there are many characters or lines. When there are many lines, you have to check where to break the line while entering the text, which can be difficult. In such cases, where there are multiple lines of text, triple-quotes are useful.
Definition of triple-quotes
Put three single/double quotation marks at the beginning and end of the block of text. ('''
or """
)
Write it like this.
'''
I am Nico.
I am Nancy.
I am Noah.
'''
Now, the part enclosed by '''
will be recognized as a string. (The same applies to double """
.)
Let's assign this string to the variable ”s" and print it out.
s = '''
I am Nico.
I am Nancy.
I am Noah.
'''
print('===============')
print(s)
print('===============')
For clarity, I have enclosed the print output with ‘===============’
. The result is shown in the figure below.

First, you can see that the string is broken into lines in the same way as when it was defined. Previously, when defining strings that included line breaks, you had to use linefeed escape sequences, but with triple-quotes, this is not necessary.
Next, what is noticeable in the output result is that there are line breaks at the top of I am Nico.
and at the bottom of I am Noah.
In triple-quotes, all line breaks that appear after '''
are traced as they are.
Remove unnecessary line breaks
There are two possible improvements for removing line breaks, and you will need to choose one of them.
- To avoid line breaks, continue writing without breaking lines (
'''
followed byI am Nico.
, thenI am Noah.
followed by'''
, without a line break). - Use escape sequences that have the effect of “Backslash and newline ignored”.
Method (1) can be written in code as follows.
s = '''I am Nico.
I am Nancy.
I am Noah.'''
print('===============')
print(s)
print('===============')
Method (2) can be written in code as follows.
s = '''\
I am Nico.
I am Nancy.
I am Noah.\
'''
print('===============')
print(s)
print('===============')
Method (2) involves placing a backslash at the position where you do not want a linefeed to be inserted (after the first '''
and after Noah.
). If there is a line break immediately after the backslash, the escape sequence “Backslash and newline ignored” is applied.
In both cases (1) and (2), you can obtain a string without unnecessary line breaks as shown in the figure below.

When single/double quotation marks exist within a string
Within triple-quotes, you can use single/double quotes without using escape sequences. Let's rewrite the I am
part of the previous code to I'm
.
s = '''
I'm Nico.
I'm Nancy.
I'm Noah.
'''
print(s)
As shown above, writing '
or "
without an escape sequence does not cause an error. Python treats all '
or "
characters that appear after '''
and before the next '''
as ‘characters’.
How triple-quotes work
Let's check how triple-quotes work. Now, let's execute the following code.
s = '''\
I'm Nico.
I'm Nancy.
I'm Noah.'''
print(repr(s))
When you execute this, you should see a result like the one shown in the figure below.

\n
is inserted where a line break is necessary. As with raw strings, you can see that the data is formatted in the same way as when defining a regular string.
Other methods of defining strings
For long strings
Suppose you have a string that is too long to wrap and you don't want to break it in the middle. In this case, we will introduce a method for defining the string in a way that is easy to read.
s = "Hello, I'm Nico. It's a great honor to meet you all. I'm originally from Los Angeles, and now I live in New York."
When a line becomes long, as in the above code, you can rewrite it as follows.
s = ("Hello, I'm Nico. "
"It's a great honor to meet you all. "
"I'm originally from Los Angeles, and now I live in New York.")
print(s)
Executing the above code will output text without line breaks, as shown in the figure below.

As shown above, the procedure for defining a string by dividing a single line of text into lines is as follows.
- Break sentences into appropriate lengths
- Enclose each line in quotation marks
- Enclose the beginning and end in parentheses
As a supplement, we will also introduce how to define using triple-quotes.
s = '''\
Hello, I'm Nico.\
It's a great honor to meet you all.\
I'm originally from Los Angeles, and now I live in New York.\
'''
print(s)
In triple-quote format, line breaks are automatically added. To prevent line breaks, you need to add a backslash at the end of each line.
You need to select the appropriate method depending on the content of the string you are defining.