美文网首页
Day2 Python如何进行多行字符串转义

Day2 Python如何进行多行字符串转义

作者: 熊熊爱吃青菜 | 来源:发表于2017-04-15 17:01 被阅读105次

    单个字符可以使用 \ 来转换,但是效率低,如何使用代码来实现呢?

    可以使用:r(raw) 来将整行的字符串进行转义

    eg:r'\(~_~)/ \(~_~)/' == '\\(~_~)/ \\(~_~)/'

    But r'.....' 不能表示多行的字符串,也不能表示包含 ' 与 "的字符串

    So 如果要表示多行字符可以使用:'''............''' 

    eg:'''Line 1

           Line 2

          Line 3'''=='Line 1\nLine 2\nLine 3'

    如果在多行字符串前面加上 r 则可以把多行字符串转化为一行字符串

    eg:r''' Python is created by "Guido".

                It is free and easy to learn.

              Let's start learn Python in imooc!'''

    eg:  '\"To be, or not to be\": that is the question.\nWhether it\'s nobler in the mind to suffer.'

    等于:print r ''' "To be, or not to be": that is the question.

    Whether it's nobler in the mind to suffer.'''

    如果不懂,可以结合Day1 来看

    相关文章

      网友评论

          本文标题:Day2 Python如何进行多行字符串转义

          本文链接:https://www.haomeiwen.com/subject/xwznattx.html