pythonformattingapostrophe

For Python language, What is the difference between single and double quotation marks


While I am developing the Python code, I found I used to use single quotation marks all the time, but some experienced programmers used double quotation marks more often. I couldn't find the difference between them, could anyone help to give me some examples if there is a difference? Or is it just a personal preference? For example:

class TextBox(UIControl):
    def draw(self):
    print('TextBox')

class TextBox2(UIControl):
    def draw(self):
        print("TextBox")

class TextBox and TextBox2 seems like have no difference while running. Thanks for your help!


Solution

  • The uses of quotation marks and double quotation marks are the same, but you can use them in different cases; I'll give some examples as follows;

    # Use of quotation mark ''
    print('This is a "python code"')
    
    # Use of double quotation mark ""
    print("This is a 'python code'")
    
    # More specific case (triple quotation mark)
    print('''"This is a 'python code'"''')
    

    If quotation mark is in a text to print, you should use double quotation mark. If double quotation mark is in a text to print, you should use quotation mark. If both of them is in a text to print, you should use triple quotation mark.

    I'd be very happy if you let me know your opinion.