Python - '' vs "", single quotes vs double quotes

In Python you can write strings with both single and double quotes. In short you can pick and choose when to use one over the other, as it gives the same result. However there seems to be an unwritten rule among Python developers on when to use single and double quotes. GeeksForGeeks puts it well: "double quotes are used for string representation and single quotes are used for regular expressions, dict keys or SQL. Hence both single quote and double quotes depict string in python but it’s sometimes our need to use one type over the other." As with many things in software development it is good to have a standard and you should follow the standard used in the project you are working on.

Some examples of strings

As mentioned you can use strings both with single and double quotes and they are interchangeable:

a = "this is a string"
b = 'this is a string'
print (a == b) #is True

You can use double quotes inside single quotes or single quotes inside double quotes:

a = "this is a 'string' in double quotes"
b = 'this is a "string" in single quotes'

Alternatively you can also escape the quotes using \, like you would in most languages:

a = "this is a \"string\" in double quotes"
b = 'this is a \'string\' in single quotes'

With triple single or double quotes you can make multi line strings:

a = """this is a string spanning 
multiple lines"""
b = '''this is a string spanning 
multiple lines'''
print (a == b) #is True

With triple quotes you can also use single and double quotes within the same string, without having to escape any of them:

a = """this is a string having double " and single quotes ' """
b = '''this is a string having double " and single quotes ' ''' 
print (a == b) #is True

That is it

I hope you found this helpful, feel free to leave a comment down below!