When you hear quotation marks in any programming language, you must’ve related it to strings. In python, strings with quotations are not something new. However, double and single quotes do tend to confuse, which to use when.
There is no difference in single and double quotations when they are not part of each other and used separately. Although, when python consoles output, single inverted commas are used, but it doesn’t affect the functionality regardless of the delimiter.
print('code leaks!') print("code leaks!")
Regardless of the use of the inverted commas, the result for both is the same.
However, if a mismatched pair of inverted commas are used, the obvious error would be raised.
Python highlights two ways to represent the strings, through single or double quotations. But when you nest a single quote into another pair of single quotes, the compiler will throw an error. This proves that there are some characters which you cannot represent in string quotation.
The compiler mistook the apostrophes as the closing quote and closes the string and the rest of the characters are not recognized. Therefore, throwing an error. Python escape single quote helps in solving this problem.
The solution is to use double quotes when there is a need to use a single inverted comma in the string as the literal text.
Another solution to this is a backslash (\) to escape the character. This tells the compiler that the character associated with the backslash (\) needs to be taken as a literal to prevent it from ending prematurely.
To use inverted commas in a string you would need the help of both single and double quotations together to make it work. there is no difference in double or single quotes, but they can be used in nesting to escape the character. The compiler ignores the backslash (\), which is another way to print sting with quotes within quotes.