Escape characters in any programming language represent how a string is formatted when it is printed onto the terminal or on any other output. When new beginners are learning to program, they often have difficulties when they are trying to print out quotation marks within a string. This post is going to show you how to do exactly that.
The content of this post includes the following:
- Error in Printing Quotes in Python Users Encounter
- Solution 1: Using Alternating Quotation Marks
- Solution 2: Using the Backslash “\” Before Quotation Marks
Error in Printing Quotes in Python Users Encounter
To understand how to print the quotes in the output, we must first look at the most common error that users encounter. To do that, take the following code:
print(stringVar)
In this code snippet, the user wants to display quotes around the word “World.” However, when this code is executed, the user is met with the following error displayed on the output:
As you can see, the output says that the user has met with a syntax error. This error happens because when the inner quotation marks are used within the string, the compiler takes it as if the main string has ended and the coming word is variable, thus causing the syntax error.
Let’s see how to avoid this error and get the required output.
Solution 1: Using Alternating Quotation Marks
The very first solution revolves around using alternating quotation marks. Basically, when the user is trying to create a string using double quotation marks, then the user can simply use single quotation marks within the string without causing the syntax error. This goes the other way around as well if the user has used single quotes for defining the strings, then double quotation marks to wrap the word within the strings.
For the example mentioned above, the correct solution is as:
print(stringVar)
When this code is executed, it produces the following results:
The output shows that the user was able to get the required output without encountering any error. Alternatively, the user can use the following set of quotation marks:
This time around, the user was able to show the double quotation marks in the output strings.
Solution 2: Using the Backslash “\” Before Quotation Marks
The second solution is to simply place a backslash in the string before the quotation marks because the backslash tells the compiler that the next character is an escape character. The correct code for the above example is as follows:
print(stringVar)
When this code is executed, it produces the following results on the output:
The output displays that the required output was successfully printed onto the terminal.
Conclusion
The quotation marks can be easily inserted into a string and printed onto the terminal by using two different approaches. The first approach includes the use of alternative quotation marks for defining the string and using inside the string. The second approach is to use the backslash before the quotation marks. Both of these solutions have been explained in this guide.