Single quotes vs double quotes in Python – When to use
In this tutorial, I will describe the actual uses of single quotes and double quotes in Python. This will help you to figure out the difference between single quotes and double quotes in Python.
It’s always better to understand through examples:
If you run this:
print('I am inside single quotes') print("I am inside double quotes")
Output:
I am inside single quotes I am inside double quotes
When should we use double quotes in Python
Now assume that you want to print this line: Opps there is single quote ' inside this string
If you put this string within single quotes you will get an error.
In this case, you have to use double quotes like this:
print("Opps there is single quote ' inside this string")
When should we use single quotes in Python
Now it’s time for the exact vice-verca. You have to print something like this: Opps there is double quote " inside this string
Here, you have to use single quotes instead of double quotes to print this type of string when there is a double quote existing in that string.
print('Opps there is double quote " inside this string')
Otherwise, if you use single quotes you will get an error like: SyntaxError: unterminated string literal
Tips: (Using triple quotes)
If your string has single quotes and double quotes, I suggest you use triple quotes to avoid unwanted errors.
For example:
print('''I am an unusal string $5^ " 1 ` ' and I love being this''')
This will kill all the errors like a pro.
Leave a Reply