. In this video, we're going to talk about
string processing. In particular, we're going to look at
several different ways in which you can manipulate strings in order to provide
interesting and useful text messages to the user.
So at this point in the course we've seen strings several times.
Okay? And we have mostly seen them in the context of printing information out to the
console, and this makes sense right? Strings are just text, so they're very
useful for conveying information to the user.
Now, while this isn't the only reason why you might use strings, it is probably the
most common. Alright? So, I have three strings here and
you'll remember that I can use both single and double quotes to represent a string.
Alright. I want to print them out and, each print statement will print out a
single line and everything that is separated by commas will be printed on the
same line separated by a space. So, let's see if this does what I expect.
Hm. Why did that not work?
Well, let's look here. We've got a single quote inside of this
string that is surrounded by single quotes.
Well, that's not going to work, cuz I thought the string ended early then there
was extra stuff. This is the whole reason that Python
allows you to use both single and/or double quotes to surround a string. This
way if I have a single quote inside the string I just use double quotes and
everything works as expected. Similarly, I could change this to a double
quote, this doesn't make any sense, but if I do that, I can put single quotes around
it and everything will work just fine. Now, obviously, that was pretty funny.
Right? No?
Okay, maybe not. [laugh].
Alright. So, let's go back to a reasonable, if not
accurate sentence. Okay?
And, You can see here that this is basically
how I can use string literals and we've seen this before.
I just wanted to remind you. Now, I can also combine strings. Okay?
I can type Warren and Rixner are nuts. Right?
And if I do that, Okay, I end up with one big string.
So, Oftentimes in a program, you're not going
to know the exact string that you want ahead of time,
So you're going to have a bunch of different strings that you combine
together and you can do this with the plus operator.
And, we know that plus in addition, we'll just add two numbers. Well, and we'll also
add two strings. And what does it mean to add two strings?
Well, as you can see here, it just puts them together one after the other.
Alright? And there's no reason why these couldn't
have been variables. Right.
If I say, A equals and, okay, I can put that in the middle of this sentence.
Okay? And everything just, it works just fine.
Right? There's no difference.
Okay? All right.
So, I can compose strings by combining them with the addition operator.
I can also get pieces of strings. So, what is at location zero at s1. You
have to actually to print that to get anything useful.
All right? And, you could see I get the character R.
Alright. So what does that mean?
Well, Computer Sciences are crazy. Okay?
And we start counting with zero. Alright.
So the 0th element of string s1, the 0th character, is R Okay? The first character,
okay, is I, second character is x, and so on, okay?
And that's all obvious. Now, Python has a little shortcut to get
to the last character, so if I can't remember how long my string is, I can type
minus one and you'll see that I get y. I'm getting the end of that string with
minus one. And I could do minus two and then I'll get
n, I'll get the second to last character, and so on.
Alright? Also, if you want to know how long the
string is you can use len. So that will tell me S1 has fourteen
characters. So that means that I can also get the last
character by putting thirteen in there. And I want to also point out, though,
You can't go past the end. So there is no fourteen character.
I'm going to get an error, IndexError, the string index is out of range.
And the same is true if I use negative to try and go off the.
Starting in the back I'll get an IndexError, okay, if I try to use a number
that's too big. Alright so let's put this back to
something reasonable. Okay.
So that's interesting, I can pull out any particular character out the string that
I'd like. I can also pull out what's called a slice.
So let's take a slice here, zero:7. Alright.
What do I get there? Well, apostrophe.
Hm. Let's think about that.
There's six characters in my name, Rixner,
Plus the apostrophe is seven characters. So I might have expected zero to seven
would have given me Rixner, But it didn't.
Okay. Why?
In Python in this slicing notation, it means go from character zero, up to, but
not including character seven. So if I wanted Rixner, I have to do eight
to get the first eight characters. Okay, and you can see that the additional
S shows up. Alright.
So, I could use this. Let's build up some strings.
Let's , six colon. Now, I want all of string s2 starting at
character six and ending at the end. So Python allows me with these slices to
have this special or if I put nothing after the colon, alright I get all the way
up to the end. Oh, hey we finally got a sentence that
makes sense, right? Okay and print s2.
I can do the same thing at the beginning that says all the characters starting at
the beginning up to but not including thirteen.
Alright, colon plus s3. Alright.
Okay. Now, we're getting some more accurate
sentences. So hopefully you can see it. I can pull
out pieces of strings. I can add them together.
I can manipulate them in this way. I can figure out the length if I need to
and I can print out single characters. Now,
Sometimes you have integers or you have strings and you want to convert them back
and forth. Okay.
So I can actually turn, I'm sorry, let me turn an integer into a string.
Okay? And use the str function to do that.
Okay? So, let's do this, You can see that this prints out 375.
It's pretty tough to tell. Is that a string or is that a number?
Well, I'll prove to you it's a string. Okay?
I'll print out a slice of it and you'll see how I get 75.
Okay? If it were a number, I could not use this bracket notation and get anything out
of it. Alright?
I can also convert this back to an integer.
Let's say that i1 equals int s5, one. Okay.
Print i1. Alright? And what that has done is that
has taken that slice, 75, and converted it back into an int with the int function.
And now, I'll prove to you that I have an integer cz I can add a number to it.
Right I couldn't do that to a string and I get 113.
Okay? So this shows some of the different things that I can do with strings.
So lets put this together to create a simple function that formats the string.
Okay. I want to write a function convert that takes one argument val and it
converts it into a nice string that tells me how many dollars and cents are
represented by that number. So lets run this.
Okay? So I give it 11.23 and it prints out
$11.23. How does it do that?
Well the first line takes the int of val, so it takes the [inaudible] number, 11.23
gets rid of the fractional part, returns eleven.
That's the number of dollars. The second line here figures out how many
cents I have by subtracting dollars from val, they give me the fractional part,
0.23 in this case. Multiply it by 100, giving you 23, and you
take the integer portion in case it had more fractional digits.
That gives me the cents. And then I use the stir function and
string addition to build up my string $11.23.
Okay, great, looks good. Now, a common error that beginning
programmers make is they look at this and say, yeah, this looks perfect, this works,
and they move on with their life and. Well, we've got a broken function here.
You probably guessed, there were a bunch of other tests here.
There must be something going on. So let's run it with these additional
tests. Alright. The second one, 11.20 prints out
$11.19. What the heck is going on there?
Alright? 1.12 prints out $1.12. Shouldn't it be dollar, $1.00 and.12.
I got $12.00 and zero cents. Even though it's 12.01.
I got zero dollars and one cents. Why am I printing out zero dollars?
I got zero dollars and zero cents for no money and so on.
Right this program has some problems okay. So this isn't such a great program.
This is why you should always test your program with more than one input.
Okay, I need to fix this simple convert function to make it work a little bit
better, I'm talking about strings here. Okay, so, let me just run some task here
and you can see that now, 11.20 prints out $11.20. 1.12 prints out Okay, I've got
$one and one cent, just one cent, $one. Dollar.
If I have no money, the computer nicely tells me, I'm broke.
I don't know if I should be happy or sad about it.
Anyway, okay, This is a much better convert function.
You see it's a little bit more complicate as well.
Okay? So I still have convert taking one
argument val. Right.
And I get dollars exactly the same way. I just get rid of the fractional part.
Now, to figure out cents, I have this statement here.
I'm using the round function instead of actually using int.
Okay. A I'dnd I leave it to you to go look at the documentation, but this takes care
of the fact that floating point numbers aren't always exactly what you expect them
to be. They're often very slightly off.
Okay? And the round function will take care of
it. It'll round the number for you.
Okay? Now.
I need to convert both of these things to strings and I would like it to not say.
Dollars if I only have one. And I'd like it not to say cents if I only
have one cent. This is a prime candidate for writing a
function because I have to do exactly the same thing in both cases.
Okay? So I've written.
A function called convert units, that actually takes the value and it takes the
name that I'm trying to print. So, in this case, the name is either
dollar or cent. It concatenates the result.
Right, it turns the value into string, adds the name, and then if, only if the
value is greater than one does it add the s.
Okay, so this is going to handle whether it should be dollar or dollars, and cents
or cent. Okay? Then I have a big conditional here that's going to check, do
I actually have any dollars, do I have any cents, and it's going to selectively print
out whether I have dollars, whether I have cents or whether I'm sad [laugh] because
I'm broke. In this video, we learn that you can do
more with strings, than just specify them in your program and print them out to the
console. You can take pieces of them.
You can combine them. You can combine pieces of them, you can
even write complex conditional statements in order to build up strings that are
appropriate for your program. Now, I'll let you decide, whose funny,
whose nuts, who wears t-shirts, who wears ties, but I do hope you learn something
about string processing. And by the way, my program told me that I
was broke. Did you all pay Coursera yet?
Cuz they never sent me a check. Should I be worried about that?