|Title||Python Tutorial: Escape Sequences In Python 3 - Python Strings #43|
|Uploader||Master Code Online|
|Type of File||Audio (.mp3) and Video (.mp4)|
Escape Sequences in Python 3
In the previous Python tutorial, we worked with strings and saw a couple escape sequences in Python 3. In this tutorial, we will focus on escape sequences in Python. We use escape sequences in Python to supply a way to include special character coding. We use the backslash(\) to indicate an escape sequence and after the backslash a letter or letters follow to indicate the proper escape sequence we need.
In my opinion, there are only a couple that you will actually ever use so we will focus on them. If you would like to see the detailed list of escape sequences visit the Python 3 documentation.
Backslash (\\) Escape Sequence
Since we use a backslash to indicate an escape sequence we need a way to produce a backslash in our strings. To do this, we use two backslashes next one another to provide the backslash in our string.
Example of Backslash Escape Sequence
a = 'This is a backslash in Python \\'
This is a backslash in Python \
In the above example, you can see that we use a double backslash(\\) to indicate a single backslash in a string in Python. What is the consequence if we only use one backslash? Well get a syntax error which indicates Python is looking another character after the backslash.
Example of Single Backslash in Python
b = 'This is not a backslash in Python \'
File "stdin", line 1 b = 'This is not a backslash in Python \' ^
SyntaxError: EOL while scanning string literal
Single Quote (\') Escape Sequence
We have seen at least one in the previous tutorial, but a review will never hurt. The single quote (\') escape sequence allows to add a single quote in our string if we are using single quotes to create(on the outside) the string. We will not have to use the single quote escape sequence in Python if we use double quotes to make the string. Look at the single quote example below.
Single Quote Escape Sequence Example
c = 'Let\'s learn Python'
Let's learn Python
In the above example, we use the single quote escape sequence to indicate to Python to treat this quote as a quote and not the end of the string. Let's take a look at what happens if we do not use the single quote escape sequence.
Example Of A Single Quote Not Being Escaped
d = 'Let's learn Python'
File "stdin", line 1 d = 'Let's learn Python' ^
SyntaxError: invalid syntax
Double Quote (\") Escape Sequence
We have also look at the double quote (\") escape sequence in the previous tutorial and it is similar to the single quote escape sequence. We only need to use the double quote escape sequence in Python when we use double quotes to create the string. If we use single quotes to create the string there is no need for the escape sequence in this situation.
Example of Double Quote Escape Sequence
e = "\"You miss 100% of the shots you don’t take.\" –Wayne Gretzky"
"You miss 100% of the shots you don’t take." –Wayne Gretzky
In the above example we use two double quote escape sequences. To allow us to use double quotes in a double quoted string. This is not a preferred method as you can see the code starts to get a bit sloppy in this situation I would use single quotes on the outside.
Bell (\a) Escape Sequence
Now you may or may not ever use this one but I like playing with it. If you add the bell(\a) escape sequence to a string your computer will make a bell noise. I have only tested this on a mac within the Python interpreter so not sure how it works with IDLE or on other operating systems.
Example of Bell (\a) Escape Sequence
f = 'Make my computer ding\a'
Make my computer ding
You should hear a ding when you print your string.
Backspace (\b) Escape Sequence
With the backspace escape sequence, we can actually remove a character in our string. I have seen that this may not work in IDLE so if does not work in IDLE you may need to use the Python interpreter in your command prompt or terminal.
Example Backspace Escape Sequence
g = '1234\b56'
Notice in the above example we remove the 4 from our string using the backspace escape sequence.
Newline (\n) Escape Sequence
The newline escape sequence will break your string to the next line where we insert the (\n) escape sequence. We have seen this when working with triple quote strings in Python.
Example of Newline Escape Sequence
h = 'This is my first line \n this is my second line \n and this is my third'
This is my first line
this is my second line
and this is my third
Horizontal Tab (\t) Escape Sequence
The horizontal tab escape sequence will tab your string in one tab space per an escape sequence. Take a look at the example below.
Example of Horizontal Tab Escape Sequence
i = 'This is a \t horizontal tab'
This is a horizontal tab
Vertical Tab (\v) Escape Sequence
The song of Python Tutorial: Escape Sequences In Python 3 - Python Strings #43 is just for review only. If you really love this song"Python Tutorial: Escape Sequences In Python 3 - Python Strings #43", please buy the original song to support author or singer of this song.