Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Python has some very powerful facilities for working with strings. And in fact, in Python 3, they become refined, and in some cases even more powerful. Let's get right into strings, and we'll start by making a working copy of variables.py. And we can call this variables-strings.py, go ahead and open that working copy, and we'll start by defining a String. I'll just call it s, and I'll say 'This is a string!' and we'll go ahead, and we'll print(s) and save that and run it.
And there we have the string. Strings in Python are immutable objects, and they're created with either single quotes or double quotes. And so if we change those single quotes to double quotes, we'll see - if we save and run, we'll see that we get exactly the same result. You can use Escape characters in a string, as you can in many languages. So if I put in here a \n, that'll introduce a new line in the middle of the string.
And if I save that and run it, we see that there is a new line now between the a and the s of string. On the other hand, if I want this \n to actually be a part of the string, rather than getting replaced with a new line, I can put the letter r before the definition of the string like that, and if I save that and run it, I get the \n as part of the string. This is called a raw string, and the place where this gets used the most is when creating regular expressions.
And we'll get into regular expressions in some detail in another chapter. In addition to these sorts of Escapes with the backslashes, you can also do some formatting and replacing of variables in the string. For example, if I create a number and call this, say n, and give it a value of 42, I could put that 42 right in the middle of the string here. And I'm going to show you the Python 3 way to do this.
And we'll look briefly at the Python 2 way to do it as well, because you'll see that a lot. For all your new code, you want to use the Python 3 way, which is with the format method of the string object, because the Python 2 way is going to go away. It's considered obsolescent, and it will be dropped in the next version of Python. So I'm going to save this and run. And you'll see what this does. This actually inserts the value of n in the middle of the string.
We have these curly braces here, and those get replaced with the format. So format is a method of the string object. And so this literal string is actually an object. Remember, everything in Python is an object. We can use this objecty-referencing operator, this period, to access a method of that object, and do this variable replacement. So this is very powerful, and this is very common.
And you'll see it done this way in new Python 3 code. In Python 2 code, you'll see it's done this other way. I'm going to put %s here, and over here a % sign and the letter n. And so we'll save that and we'll run it. And you see we get the same result. This is the way that it was done in Python 2. And this is a bit of a hack. It's perfectly valid, and you'll see it a lot.
You'll even see it some in Python 3 code that's written by people who are used to Python 2, and that's fine. The reason you don't want to use this construct is because it is considered obsolescent, and it will be dropped in the next version of Python. So you'll see this, you want to know what it does, but the right way to do it in Python 3 is to use the new format method of the string object.
And that will look like this. I'll save that and run it. And that's how that works. There's one more way I want to show you for defining a string, and this is using the triple quotes. And you can triple either the single quotes or the double quotes. So I can either do it this way, or instead of these triple quotes, I can do it this way with the double quotes, like this. And you'll see it's done both ways. I tend to use the singles tripled instead of the doubles tripled.
Now what this does is it allows you to have a string that spans several lines. And so, what I'll often do is I'll start with a backslash and a new line and go all the way back to the end of the line like that, and this will allow me to just have line after line of text and more text, and to have it actually started at the beginning of it.
I'll explain what this does in a moment. Let's save this and run. And then you can see what this does. This is useful if you have lines and lines and lines of text. And so the way you do this is with three quotes, either single quotes or double quotes, at the beginning and the end. And the result will be a string that has the new lines, and it's all formatted exactly like you type it in. What this here does, this backslash and a new line, is that it escapes the new line, so that it doesn't actually show up in the string.
If I didn't have this, and I save this and run it - that's scrolled out of the way - there you see that we get a black line at the beginning, because this blank line here is actually inserted in the string. The way to get that not to happen is to use the backslash before it. The backslash has to be the very last character on the line, so that it's escaping the new line and not a space or something. So I save that and I run it, and you can see that we don't have a scrollbar over here. This is actually at the top, and there is no new line at the beginning of the string.
So that's the triple quote way of defining strings. And that's often used in docstrings in function, which we'll get to later on in the chapter on functions.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97511 Viewers
61 Video lessons · 84750 Viewers
71 Video lessons · 68928 Viewers
56 Video lessons · 101379 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.