If a string contains many characters that need to be escaped, it can be cumbersome to escape each character. To avoid this, we can prefix the string to r
indicate that it is a raw string, and that the characters inside it do not need to be escaped. For example:
R ' \ (~_~)/\ (~_~)/'
However r‘...‘
, the notation cannot represent multiple lines of string, nor can it represent ‘
"
a string containing and (why?). )
If you want to represent multiple lines of string, you can use the ‘‘‘...‘‘‘
expression:
"Line 1Line 2Line 3"
The above string is represented in exactly the same way as the following:
' Line 1\nline 2\nline 3 '
You can also add the multiline string to a raw string by adding it in front r
of it:
R ' ' Python is created by ' Guido '. It's free and easy to learn. Let's start learn Python in imooc! '
Raw strings and multiline strings in Python