![]() Here, we have listed all valid escape sequences used in Python program that are in the below table: Escape SequenceĮxample Program based on Escape Sequence Characters The symbols \ and ” together represent an escape sequence, but those two symbols represent a single character. The interpreter interprets the combination of two symbols in the escape sequence as a whole, rather than individually. In this example, we have used an escape sequence that is a combination of the backslash and the double quote. ![]() But if you need to use both kinds of quotation marks in a string? In this case, we can do like this: print("He said \"Python is fun unlimited\"") If you need to put a double quotation marks in a string literal, you can easily apply single quotation marks around the string. print('He said "Python is fun unlimited"') ![]() However, we can also use a single quotation mark around a string containing double quotation marks. One simple method to fix it is to use double quotation marks around the string, like this: print("He said 'Python is fun unlimited'") When the Python interpreter encounters the second quote, then it will think that the second quote is the end of the string and does not know what to do with the rest of the characters that come after the second quote. Print('He said 'Python is fun unlimited'')Īs you can see, the interpreter generates a Synta圎rror: invalid syntax. If we write it directly, an error occurs: print('He said 'Python is fun unlimited'')įile "C:\Python Project\EscapeSequence.py", line 1 Suppose we want to display a message with a single quotation mark in the output. Let’s understand it with the help of an example. We generally use an escape sequence to control printed or displayed output. It tells the Python interpreter to interpret the next character(s) in a specific way and ignores its usual meaning. In Python, the backslash (\) is known as escape character.
0 Comments
Leave a Reply. |