What are Escape Sequences?
Escape Sequences refers to the combination of characters that begin with the backslash(\). They are non-printable characters which convey some special meaning to interpreter like to change a line , leave seven spaces etc.An escape sequence is used when writing sections of code, like preprocessors definitions, to specify continuation characters,so that multiple lines of codes are consider as a single line by interpreter.
Escape Sequences are basically used for designing purposes. They can be used between the statements. An escape sequence contains more than one character but functions as a single character because there is no letter to textually represent escaped character.
How to use Escape Sequences?
The following is the list of some Escape sequences with the examples:
- \n
It is used to change the line. You can use it anywhere in the middle of statement.
eg. >>> print("I am here .\n You must come here"
Output: I am here.
You must come here
- \t
It is used to give a tab(seven spaces) in between two words in a single line.
eg. >>> print("I am here .\tYou must come here")
Output: I am here. You must come here
- \\
It is used to print a backslash(\) or to ignore some escape sequence.
eg. >>> print("\\")
Output: \
eg. >>> print("\\t got ignored")
Output: \t got ignored
eg. >>> print("C:\Users\name\AppData\Local\\testfile")
Output: C:\Users\name\AppData\Local\testfile
- \b
It is used to remove a character that is written previous to \b.
eg. >>> print("Removing a characters\b.")
Output: Removing a character.
- \f
Special character.
eg. >>>print("Hello World\f")
Output: Hello World
- \'
Prints a single quote '
eg. >>>print(" \' ")
Output: '
We have seen some important escape sequences that we will be using.Some of the other escape sequences, we will be seeing in our next tutorials.
Comments
Post a Comment
Please do not enter any spam link in the comment box