Strings
- [Instructor] All right, this is an exciting one. In Python, we're working with strings a lot, whether it's parsing them to extract some data or constructing them to provide some information to an end user. And Python has a lot of tools to both analyze and construct strings. And the first one I want to take a look at is called slicing. It's called slicing because we're literally taking a slice out of a string and returning it. So let's make a string. "My name is Ryan Mitchell." Okay, and let's say we want to get just the first character of that string. We can do name zero. Remember, programmers always start counting from zero, so we say that m is at index zero in the string, and we could also get the second character, name one, which is that y at index one in the string. Well, what if we want to get the first seven characters of a string? The words my name. We could use a colon, zero colon seven, which means get me the characters between zero and index seven. Note that it doesn't actually get the character at index seven, which is the space after name. It gets the characters up to index seven. And there's also this sort of shorthand you can use with the slicing syntax, which is name seven. If you want to write a zero here, you can just leave that zero out and it's equivalent to writing the zero. Similarly, if you want to get all the characters from index 11 to the end of the string, you can just do 11 and then leave that out. And this is really handy because we may not know the length of the string, so you don't actually have to write it in if you don't know it. It just goes up to the end of the string. So if you leave it out at the beginning, it starts at the beginning of the string. If you leave it out at the end, it goes up to the end of the string. It's also important to note that the slicing syntax is exactly the same when you're working with lists. So if I have a list, one, two, three, four, five, I can take a slice of that list. My list, two to four. Lists and strings actually have a lot of similarities in Python. So I can take the length of a string. So the string has 24 characters, I can also take the length of my list. My list has five elements. We're going to be looking at some other neat features of the slicing syntax later on when we look at lists. And so keep in mind that all these features can be used in the exact same way with strings too. So let's look at a few things now that are particular to strings. So Python is great at string formatting. There are a few ways to create strings in Python, including one very basic obvious way with string concatenation. So, let's do my number is string five... So my number is five. We can do this with something called an f string. Simply type in f, which stands for format, then type your string without any spaces between this f and your string. So put your variables inside curly braces. My number is five. Same exact thing. You can also write expressions inside the curly braces. So f my number is, let's say two. Let's do five, and twice that is two times five. Whoops. There we go. You can even do rounding and number formatting. If we import the Python math library by convention at the top of the file, we have access to math.pi which is pi out to 15 decimal places. And we can display pi rounded to two decimal places with a colon and a 0.2f. So let's just write this, pi is math.pi, that's going to be pi to 15 decimal places. And then using the special string formatting syntax, we're going to round it to two decimal places. So f strings came out with Python 3.6. It's a relatively new feature. Prior to version 3.6, the string format function was used which is very similar. You can see the format function. I prefer f strings now, but let's just take a look at it. Pi is .format math.pi. And finally, a new way to write strings. The multi-line string. If you use three quotes, Python will go into multi-line string mode. If I were to put a new line in the string up here, it would return a syntax error. Yep, no good. However, sometimes you want to include new lines in your strings. So my string here is a long block of text. I can add new lines. The text doesn't stop until it sees... Oh, look at that. What if I want to put literal, triple quotes in there? I can use that backspace character to escape it. And if we print out my string, there we go. You see that this actually includes these backslash ends in here. This backslash ends are new lines. This indicates that we're going to a new line of text. And this is just what Python prints out when you're actually looking at the string. If I do print my string, you can see it renders it a little more nicely. So Python is fantastically flexible at manipulating, parsing, and creating strings. There are hundreds of string functions and ways to use them. We'll be covering more ways to use them, but if you want to look for more information in the meantime, I suggest checking out the official documentation on Python's website.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。