Multiline strings in python

Raw string and multi-line string in Python

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 indicate that this is a raw string, and the characters inside do not need to be escaped. E.g:

r'\(~_~)/ \(~_~)/'

But r'...'representation can not represent multi-line string can not contain express 'and "strings (Why?)

If you want to represent a multi-line string, you can use the '''...'''representation:

'''Line 1
Line 2
Line 3'''

The representation of the above string is exactly the same as the following:

‘Line 1\nLine 2\nLine 3’

You can also add in front of a multi-line string and turn this multi-line string into a raw string :

r'''Python is created by "Guido".
It is free and easy to learn.
Let's start learn Python in imooc!'''

Leave a Reply

Your email address will not be published. Required fields are marked *