Escape a single quote (') in the raw string r' ... '

I want it to print

this is '(single quote) and "(double quote)

I use the following (I want to use the raw string 'r' here)

a=r'this is \'(single quote) and "(double quote)' 

but he prints

this is \'(single quote) and "(double quote)

What is the correct way to escape 'in the source line?

+4
source share
2 answers
>>> a=r'''this is '(single quote) and "(double quote)'''
>>> print(a)
this is '(single quote) and "(double quote)
+4
source

Quote from Python String Literal Docs ,

'r' 'r', , , , . , r"\n" : 'n'. , ;, , r"\"" - , : .

  • , Python Strings

    print(r"""this is '(single quote) and "(double quote)""")
    # this is '(single quote) and "(double quote)
    
  • ,

    print(r"this is '(single quote) and " r'"(double quote)')
    # this is '(single quote) and "(double quote)
    
+2

All Articles