How to run your first python program:

We’re going to be discussing strings or sequences of characters using the syntax of either single quotes or double quotes.

Here we can see three examples. First, we have HELLO of single quotes than hello. Double quotes. And then what’s nice about having both options of single quotes or double quotes.

It means that if you have a single quote in your string that you want to keep and not have that end your exact string, you can wrap it in the other type quote such as double quotes or vice versa. So here we can see I don’t do that has a single quote in there but we don’t want that single quote to suddenly end the string so we can wrap the whole thing in double quotes. Now we’ll see an example of that later on in the Juber notebook. Now it’s important to note here is that strings are ordered sequences, and that means we can use indexing or slicing to grab subsections of the string because we know each character has a specific position to be in and indexing notation eases that square bracket notation after the string or the name of the variable

assigned to the string. Now we’re going to see lots of examples of indexing slicing in just a little bit. So indexing is the term used when you want to grab a single character from the string. So, the way this works is that every single character has an index position assigned to it. So, you start at zero. That’s another important note in Python that indexing starts at zero. So, H has a corresponding number of zero E has a corresponding number of 1 2 3 0 4. So, if I wanted to grab the E What I would use is inside the square brackets I would pass one after the string, and then it would return E, and I will see examples of that later on.so if you want to learn the basic about sting in python you can download the pdf from Techstack website .techstack is one of the best institute for python and machine learning courses in Delhi NCR

What’s also interesting about pi thumb is you can use reverse indexing. So maybe you wanted to grab the last letter of a string, but you didn’t know how long the string was. All you knew was you wanted to grab the last letter. Well, luckily there’s reverse indexing available to you so you can grab negative one it’ll grab the last letter of the string regardless of how long that string is slicing allows you to grab a subsection of multiple characters otherwise known as a slice of the string. And this has slightly different syntax.

Again, it’s going to be in square brackets because we’re grabbing a subsection. We’re going to be able to define three parts of this we’re going to be able to say start-stop and step. So again, this goes with square brackets with a colon separating each of these three terms. Star is going to be the numerical index of the slice. Start-stop is going to be the index you will go up to but not include. That’s an important note here. And we’re going to focus on that and the examples we’ll see in just a bit and then that is the size of the jump you take from start to stop. OK so let’s explore all these concepts.

They’re going to make a lot more sense when we see the code examples. Let’s hop over to a notebook. OK let’s quickly show a couple of examples of a string. Again we can use single quotes, hello or you can use double quotes. So here I say double quotes of world. You can also have an entire phrase doesn’t need be one word so we can say this is also a string. So we have a whole phrase there. And the white spaces count as characters inside of the string. Now, something we should note here is that we can mix single quotes and double quotes. So if I and say something like for instance I’m going on a run.

So what’s happening here. Because I’m using single quotes on the outside only part of this is getting a highlight of the syntax is going to confuse Python because it thinks that you’re trying to end the string here when I’m trying to end the string here. So if I try to run the cell will say an error here. Invalid syntax. What I want to do is wrap this in double quotes and that way I won’t have an error when I have this single quote in here. And then when I run this python has no problem and says OK I get we are trying to do here trying to have a single quote there stay, and that’s not part of the definition of the string.

So now let’s discuss printing out a string. So far we’re just asking the string to be returned, and that’s the reason we see in and out with these cells. That’s also the reason we see the quotes in the output below the cell. But we can use the print function to print out a string. So we’re going to say prints hello. And if we run this note well we get back we no longer see the out in the cell. And instead, we no longer see the quotes themselves.

We’re just printing out the actual string. And the reason this is important is that let’s imagine I wanted to say hello world one, and then I also wanted to say hello world two if I were to run this. What ends up happening is I only get back that last string to see everything. I have to print out the results so that I will say Prince and raptus in print see prints hello world one that also prints and then say Hello World 2. And then when I run this, I get to see bowstrings printed out.

So I no longer see the output. I’m just printing the results. Now soughing also wants to mention is that there are escape sequences and escape sequences like to have special commands inside of your string. So let’s go ahead and say, Prince. Hello, world. And if I run this, I see Hello World printed on one line, but I can add an escape sequence. So it’s going to be a backslash n and what this does is it basically tells Python hey this and right here that’s no longer the character n I want you to because of this backslash. Treat this as a new line. So if that escape character there, it’s going to say Prince Hello space than a new line and then space world. So that’s what we see here.

Hello, space and world if I want the world to be lined up. Hello. I could touch it like this. Run that and then I see Hello world in Python knows not to include this letter end there because it’s essentially attached to that backslash. So another popular escape sequence is t for tab. And if I run that I get back. Hello, tab. So four spaces world. Well, we’re talking a lot more about this when we discuss print formatting coming up next. For now, another built-in function that I want to show you is the Eliane function or the length function.

So this allows you to check the length of the string so I can say hello and higher on this I get back length of 5 because there are five characters in that string. If there happens to be a space in the string, so it will say I am hungry. Let’s make it more obvious we’ll say I am. We run this. Here we can see there’s four. So we have I Space am. So that counts as a length of four characters in the string. All right. We’ll stop here for now. In the very next blog, we’ll pick up right where we left off discussing string indexing and string slicing. I’ll see you there.

Leave a Reply

Your email address will not be published. Required fields are marked *