We know that if we want to print something on the console then we use print() statement in python. In a print statement, we can use single quotes or double quotes to print the text to the console.
In this article, we will see what is the difference between single and double quotes in Python and in which scenario we have to use one.
In languages like Java, C++, and C when we want to print something we strictly have to use double quotes. This thing is different in the case of Python Language. In Python, we can use either single quotes or double quotes.
Difference between Single and Double Quotes in Python
In the case of Python, there is no major difference between the quotes. We can use either of them in the print statement.
Now, we will see some scenarios that why both the quotes exist in Python while not present in other Languages.
Let’s suppose we want to print a statement like This is Hari’s Book.
By using double quotes we can easily print this statement.
Code:
print("This is Hari's Book.")
Output:
This is Hari's Book.
But when we try to print the same statement using single quotes then we will get an error.
Code:
print('This is Hari's Book.')
Output:
File "c : \ Users \ ASUS \ Desktop \ Crazy Programmer Work \ test.py " , line 1 print('This is Hari's Book.') ^ SyntaxError : invalid syntax.
Here it is showing an invalid syntax. Let’s see why this occurred.
As we can see in the code we have used single quotes just before This and we also used single quotes after Hari and after Book.
But we know that if we have opened a single quote then we have to close it also, and the interpreter assumes the single quote after Hari as their closing single quote. That is why it is giving errors to us.
Now let’s see the solutions for the above error.
Solution 1: By Using Escape Sequence Characters
We can use a special character called Escape Sequence in our statement when we want to use any of the quotes in the statement.
Code:
print('This is Hari\'s Book.')
Output:
This is Hari's Book.
Now we can easily print the required statement without any error.
The escape sequence tells the interpreter to escape the next character, so the single quote after Hari is not concluded as a closing quote.
Solution 2: By Using Opposite Quotes
If we want single quotes in our statement then we have to use double quotes in the print function and vice-versa. That means if you want a double quote in your printed statement then you have to use single quotes in the print function.
For the above statement as we can see we want a single quote in our statement then we have to use a double quote in the print statement.
Code:
print("This is Hari's Book.")
Output:
This is Hari's Book.
If we want to see a double quote in our statement then we have to use single quotes in the print function.
Code:
print('This is Hari"s Book.')
Output:
This is Hari”s Book.
Conclusion
We can use a single quote or double quote in the print function to print a statement in python. But if we want a single quote in our statement, then we strictly have to use double quotes in the print function. To perform this action, we can also use escape sequence characters.
If we want a double quote in the statement, then we strictly have to use a single quote in the print function.
The post Python Single vs Double Quotes appeared first on The Crazy Programmer.
from The Crazy Programmer https://ift.tt/0EFdCRH
Post a Comment