HomeLinuxPython String Escape Quotes

Python String Escape Quotes


Escape characters in any programming language symbolize how a string is formatted when it’s printed onto the terminal or on another output. When new newcomers are studying to program, they usually have difficulties when they’re attempting to print out citation marks inside a string. This publish goes to indicate you how you can do precisely that.

The content material of this publish consists of the next:

Error in Printing Quotes in Python Customers Encounter

To know how you can print the quotes within the output, we should first take a look at the commonest error that customers encounter. To do this, take the next code:

stringVar= “Whats up “World” That is LinuxHint!”

print(stringVar)

 

On this code snippet, the person needs to show quotes across the phrase “World.” Nonetheless, when this code is executed, the person is met with the next error displayed on the output:

As you may see, the output says that the person has met with a syntax error. This error occurs as a result of when the inside citation marks are used inside the string, the compiler takes it as if the principle string has ended and the approaching phrase is variable, thus inflicting the syntax error.

Let’s see how you can keep away from this error and get the required output.

Resolution 1: Utilizing Alternating Citation Marks

The very first answer revolves round utilizing alternating citation marks. Principally, when the person is attempting to create a string utilizing double citation marks, then the person can merely use single citation marks inside the string with out inflicting the syntax error. This goes the opposite method round as nicely if the person has used single quotes for outlining the strings, then double citation marks to wrap the phrase inside the strings.

For the instance talked about above, the right answer is as:

stringVar= “Whats up ‘World’ That is LinuxHint!”

print(stringVar)

 

When this code is executed, it produces the next outcomes:

The output exhibits that the person was in a position to get the required output with out encountering any error. Alternatively, the person can use the next set of citation marks:

This time round, the person was in a position to present the double citation marks within the output strings.

Resolution 2: Utilizing the Backslash “” Earlier than Citation Marks

The second answer is to easily place a backslash within the string earlier than the citation marks as a result of the backslash tells the compiler that the following character is an escape character. The right code for the above instance is as follows:

stringVar= “Whats up “World” That is LinuxHint!”

print(stringVar)

 

When this code is executed, it produces the next outcomes on the output:

The output shows that the required output was efficiently printed onto the terminal.

Conclusion

The citation marks may be simply inserted right into a string and printed onto the terminal by utilizing two totally different approaches. The primary strategy consists of the usage of different citation marks for outlining the string and utilizing contained in the string. The second strategy is to make use of the backslash earlier than the citation marks. Each of those options have been defined on this information.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments