[004] Introduction to Python Basics: Escape Sequence, formatted string, string indexes
Let’s talk about something called escape sequence in Python.
- In the string, we will be writing about the weather and we want to write “it’s sunny”. Well, you notice that how the highlighting on the string. With the It(‘s), the Python is reading the start and end of the string on the “It” word. How can we fix this?
- We can add the double quotes to fix it and now it will work.
- Now we have another issue. How can we solve this? This is because in human language we have double quotes and apostrophes.
- We can use escape sequence to solve it and this will be a little hard to understand at first. We just simply added a slash (\) which is the one that are right above the enter in the keyboard.
- Whatever that comes after the string, it will be assumed as string.
- The backslash will be added in front of the single quote or double quote string. What comes after the \’ or \” is a string.
- Here when we print the weather, it is printed out nicely. The escape sequence can be used in multiple ways.
- For instance, if I want to have an actual backslash in the string, we can put double backslash(\\) as the second backslash will be known as a string.
Some neat tricks used with backslash:
- The \t is added in the string and it yields a string with a tab spacing infront of the string.
- The \n will denotes a new line in the string. These escape sequence is kinda hard to see at first but it is pretty much available in all programming. Just as usual, the escape sequence of Python can be found in the documentation.
Formatted Strings:
Up till now, we just written simple strings but we want a program that is dynamic not static. When we log into their profile, we want to display their name. In this case, we don’t want to hard code every single user’s name. Ideally, we will have something that are dynamically.
Let’s assume that the name field will grab the name from the database after the account is created.
- We get an type error where the type cannot be int.
- Convert the int into string with the str(). This is quite troublesome and we have better way to write this in formatted string.
- With the formatted string, the f is added at the beginning. The f will be telling the Python that is going to be a formatted string instead of all the addition and str().
- This is a new feature to Python 3 by adding f to beginning which make the variable available inside the string. The code is now more clean and neat.
- This is another way that works in the same way as adding f at beginning.
- The variable is also work in the format bracket.
- We can mix the two variable by adding the number in the {}. In computer science, we start counting from 0. Thus the sequence for name is 0 not 1.
- We may also create our own variables and put into the {}. the dot format is a little bit more complicated. It is frequently used in Python 2 and maybe some programmers still prefer this in Python 3. Personally, I prefer the formatted string with the f at beginning when compared to the dot format.
String Indexes:
The string will be stored in the computer memory in order manner. If you think of a book shelf and each one of these is ordered in different part of the bookshelf. The way that we can think is like this:
- Each number is corresponds to each letter. This is crucial and important as the most useful things that we can do with strings is to access different part of a string. This can be done with what we call an index.
- We will print the variable hi with the indexes of 0 and get the letter W. Using the square bracket, we will grab whatever is an index of 0 in the hi variable which is W.
- When we getting the hi variable and it will get the letter from the index of zero to seven. With this knowledge , we can do a lot of string manipulation in Python.
Let’s look at the unique feature:
- The string will be changed to “01234567” for the ease of explanation.
- When we use square brackets in Python, the first item that we put in between the bracket is start. This tell us where do we want to look.
- When we add on colon behind the start, we also have the option to decide where to stop. In the example, we start at 0 and stop at 2(not include). This yields a result of 01.
- We are able to print all the number if we stop at 8. This is because there are 8 characters since we start from 0.
- Third we can add in the bracket which is the STEP OVER. It will start at one point, then end a one point and step over few characters. The default is one since we are moving through the list one by one and we get the entire string.
- Step over by 2. We start at 0 then step over 0 and 1 to get 2. Then, we step over 2 and 3 to get 4. Next, we step over 4 and 5 to get 6. This yielded the result of “0246”.
- In this example, we start at 1 till the end (default) even though the end is not stated since the default step over equal to 1.
- This example, we start at default which is 0 and stop at 5 since the step over equal to 1.
- In this example, we start at default 0 till the default end which is till the end of string with step over of 1.
- The negative index in Python means start at the end of string.
- In this example, we step over from the end of string and we get the reverse of the string(reverse order). This is a very common, unique and useful operation to Python.