In this video, we're going to explain what a string is in Python
Video Transcription
Hi, my name is Art and I teach Python at Noble Desktop. Today I'm going to explain about strings. What is a string? So, Python comes with built-in data types and string is one of them.
Strings are very important data types. Before I give you a formal definition, let's create a string. So, first of all let's create a variable name, let's say "word" and then to initialize the string or to create a string, we need to use quotes. All right, and then let's say "Apple".
By the way, I always get this question asked: should I use single quotes or double quotes? Now, the answer is you could use them interchangeably as long as you are being consistent and as long as you open with a double and close with a double. If you want to use single, that's going to be exactly the same stuff. So, you could open it with a single and close it with a single. Right, so you could use any type of quotes.
All right, so what is a string? So, string is a sequence of characters. It's a sequential data type. Now, why I'm calling them characters is because that's "Apple", but keep in mind that Python has no idea what we're talking about here. For Python, it's just a bunch of characters. And you could use function type to check the data type of this data type which is a string. Also, we could do something silly here. I could add a bunch of numbers or special characters. If you re-run this, you'll see that it's still a string. So, again keep in mind that that's a sequence of characters.
All right, so Python doesn't care if those characters or letters make any sense to us, humans. Another thing that I want to tell you is that every object in Python has built-in methods. So, how do you find them? What you could do is use function dir and to make it a little bit cleaner and to fit everything on the screen, I'm going to use function print.
All right, so print dir is one of the built-in functions and then you immediately see all available methods that are already built into that object. So, for example, there is a method "upper". How would you use those methods? It's pretty simple, you take that object and then you use "upper" and then you'll see it will convert this to uppercase. Or, another option you could convert it or you could capitalize it. You could use "capitalize" and now it will be capitalized.
So, the point I'm trying to make is when you work with an object such as a string, it would be smart to check all the built-in methods that are already built into that object so you could grab them and use them right away. All right, so what else can we say about the string?
Since it's a sequence of characters, we could index it. So, indexing means grab a character one at a time. So, in programming, we started counting at zero. So, if I want to grab "A", then I can pass the index of zero and then I'm going to get "A". All right. And that would pretty much work on any string because they share the same features. So, if it was "banana", then I would get "B". If it was "orange", then I would get "O". All right, so you could index it. And by the way, if you want to get the next letter, you could move and you could use one and that will get you "r". Two will get you "a" and so on. I hope you got the picture.
Also, you could slice it. So, for example, if I want to get back to "Apple", it will be simply to explain. So, "Apple". So, if I want to grab "P" and "P", then I need to use slicing. I need to provide a start point and a stop point. All right, now the start is pretty straightforward. So, that would be one if I'm trying to get "PP", because "A" is let's see what "A" is. "A" is zero, so I'm trying to get "P". So, that would be one. Now, the stop is a little bit tricky. So, stop is exclusive, it's not included. So, when you stop, you gotta go one more. So, then instead of using colon two, because that would get me the same "P" that I already have here, I gotta go one more. So, I gotta use three and I'm going to get "PP". So, bottom line, you have to understand that although "Apple" has some meaning to us, humans, for Python, it's just a sequence of characters. So, string is a sequential data type. It's a container that holds a bunch of characters. Could be alphabetical characters, could be numerical characters, could be special characters. Anything that you wrap in quotes would be stored as a string.
So, watch my other videos where we're going to discuss more built-in data types. Thank you for watching.